您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 企业文化 > 02 J2ME UI 1
J2MEUserInterfaceI2J2MEUserInterfaceIMajorclassesinthelcduipackageTobediscussedinthislecture3J2MEUserInterfaceITextBoxThesimplesttypeofscreenistheTextBox.TextBoxallowstheusertoenterastring.Textinputisadifficulttaskonmobilephones.Manydevicesonlyhaveanumerickeypad,soenteringasinglecharacterisamatterofone,two,threeorfourbuttonpresses.AgoodMIDletrequiresminimaluserinput.e.g.anemailTextBox4J2MEUserInterfaceITextBoxATextBoxiscreatedbyspecifyingfourparameters:publicTextBox(Stringtitle,Stringtext,intmaxSize,intconstraints)ThetitleisusedasthescreentitleThetextandmaxSizedeterminetheinitialtextandmaximumsizeofthetextbox.Theconstraintsareusedtorestricttheuser'sinput.–ANY:allowsanytypeofinput.–NUMERIC:restrictstheinputtointegers.–DECIMAL:allowsnumberswithfractionalparts.–PHONENUMBER:requiresatelephonenumber.–EMAILADDR:inputmustbeane-mailaddress.–URL:inputmustbeawebaddress.5J2MEUserInterfaceITextBoxConstraintsThedevicesdon'tallowinvalidinput;forexample,aNUMERICTextBoxdoesn'tallowyoutoenteralphabeticcharacters.Constraintsmaybecombinedwiththeflagslistedbelow.Constraintslimitthebehaviorofusers,whileflagsdefinethebehavioroftheTextBox.Theavailableflagsare:PASSWORD:charactersarenotshownwhenentered;generally,theyarerepresentedbyasterisks.UNEDITABLE:indicatestextthatcannotbeedited.6J2MEUserInterfaceITextBoxFlagsSENSITIVE:indicatesthattextshouldnotbestored.Someinputschemesstoreinputfromtheuserforlateruseinautocompletion.Thisflagindicatesthatthetextshouldnotbesavedorcached.NON_PREDICTIVE:indicatesthatyouareexpectingtheusertoentertextthatanytext-predictinginputschemewillprobablynotbeabletoguess.Forexample,ifyou'reexpectingtheusertoenteranordernumberlikeZ51002S,youwouldusethisflagtotelltheinputschemetonotbothertryingtopredicttheinput.INITIAL_CAPS_WORD:isusedforinputwhereeachwordshouldbecapitalized.INITIAL_CAPS_SENTENCEindicatesinputwherethefirstcharacterofeachsentenceshouldbecapitalized.NOTallofthesesettingsmaybefunctionalinalldevices.7J2MEUserInterfaceITextBoxFlagsTheflagsmaybecombinedwithanyoftheotherconstraintsusingtheORoperator(|).Forexample,tocreateaTextBoxthataskstheusertoenteranumberpassword,youwoulddosomethinglikethis:Displayabled=newTextBox(PIN,,8,TextField.NUMERIC|TextField.PASSWORD);8J2MEUserInterfaceIExample:AcceptastringfromTextBoxandechoitOneTextBoxTwoCommands-ExitandGreetOneCommandListener9J2MEUserInterfaceIExampleimportjavax.microedition.midlet.*;importjavax.microedition.lcdui.*;publicclassTextBoxTestextendsMIDletimplementsCommandListener{privateDisplaydisplay;privateTextBoxtbClip;privateCommandcmExit;privateCommandcmGreet;publicTextBoxTest(){cmExit=newCommand(Exit,Command.EXIT,0);cmGreet=newCommand(Greet,Command.SCREEN,1);tbClip=newTextBox(TextboxTest,,20,TextField.ANY);tbClip.addCommand(cmExit);tbClip.addCommand(cmGreet);tbClip.setCommandListener(this);}10J2MEUserInterfaceIExamplepublicvoidstartApp(){display=Display.getDisplay(this);display.setCurrent(tbClip);}publicvoidpauseApp(){}publicvoiddestroyApp(booleanunconditional){}publicvoidcommandAction(Commandc,Displayables){if(c==cmExit)notifyDestroyed();elseif(c==cmGreet)System.out.println(Hello+tbClip.getString());}}11J2MEUserInterfaceIAlertsAnAlertisessentiallyasimpledialogbox.TherearetwotypesofAlert:–modal,whichdisplaysthedialoguntilacknowledgedbytheuser,and–timed,whichisdisplayedforaspecifiednumberofseconds.TheconstructorsforanAlertareshownbelow:Alert(Stringtitle)Alert(Stringtitle,StringalertText,ImagealertImage,AlertTypealertType)Youcanspecifyanimagebutwewilldiscussitinlaterlecture.12J2MEUserInterfaceIAlertsTheAlertTypeclassprovidesfivetypes:ALARM,CONFIRMATION,ERROR,INFO,andWARNING.TheAlertTypecomponentusessound,ratherthananimage,tonotifytheuserofanevent.Bydefault,timedAlertsarecreatedusingadefaulttimeoutvalue;youcanfindoutthedefaultvaluebycallinggetDefaultTimeout().Tosetthetimeoutvaluetofiveseconds,youcoulddothis:alert.setTimeout(5000);Ifyouwantamodalalert,usethespecialvalueFOREVER:alert.setTimeout(Alert.FOREVER);13J2MEUserInterfaceIExampleFiveAlertsThefollowingexample,FiveAlerts,showsalltypesofalert.Thedisplayhassixcommands(5Alerts+1Exit).Thedefaulttimeoutvalueis2000ms=2seconds.i.e.TheAlertscreenwilldismissafter2seconds.14J2MEUserInterfaceIExample-FiveAlertsTheErrorAlertwillstayuntiltheuserdismissit.TheInfoAlertwillstayforonthescreenfor4seconds.Afterthealertdismisses,thedisplaywillreturntothepreviousscreenpublicvoidsetCurrent(Alertalert)orgotonextscreenifthefollowingsetCurrentisused:publicvoidsetCurrent(Alertalert,DisplayablenextDisplayable)15J2MEUserInterfaceIExample-FiveAlertsimportjavax.microedition.midlet.*;importjavax.microedition.lcdui.*;publicclassFiveAlertsextendsMIDletimplementsCommandListener{privateDisplaydisp;privateFormf;privateAlertalarm;privateAlertconfirm;privateAlerterror;privateAlertinfo;privateAlertwarning;privateCommandalarmCommand,confCommand,errCommand,infoCommand,warnCommand,exitCommand;publicFiveAlerts(){alarmCommand=newCommand(Alarm,Command.SCREEN,1);confCommand=newCommand(Condirm,Command.SCREEN,1);errComm
本文标题:02 J2ME UI 1
链接地址:https://www.777doc.com/doc-3748436 .html