How to use pop-up TextBox in Java ME
Overview
OneoftheDisplayablesinLCDUIisTextBox(extendingScreenclass),whichallowsusertoenterandedittext.Itiscommonlyusedforenteringrelativelyshorttexts,evensinglewords.InanycaseTextBoxhasusedthewholescreen,whichhasmadeuserexperiencebad.NowinS605thEditionnewmodeofpop-upTextBoxisintroduced.ByusingaJADattribute"Nokia-UI-Enhancement"withvalue"PopUpTextBox"alltheTextBoxscreensareshownassmallerdialogs,withoutobscuringtheunderlyingscreen.
Nokia-UI-Enhancement:PopUpTextBox
Pop-upTextBoxdoeslackhavesomepropertiesof"traditional"TextBox:
Tickerisnotvisible
Textinputcapacityindicatorisnotsupported
AnemptyPop-upTextBoxhasoneline,butifneeded,itssizewillgrow.Theexactmaximumamountofvisiblelinesdependsonthescreensize.InnHDscreens(640x360pixels)itis5rowsoftext.InputtingandeditingtextispossiblebytappingontheTextBox.
Theimagebelowshowsanemptypop-upTextBoxontopofCanvas(innormalmode)andapop-upTextBoxwith5rowsoftext
HereisasimpleMIDletdemonstratingpop-upTextBoxfeature.
[edit]
Sourcecode:PopUpTextBoxMIDlet.java
importjavax.microedition.midlet.MIDlet;
importjavax.microedition.lcdui.Alert;
importjavax.microedition.lcdui.AlertType;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
publicclassPopUpTextBoxMIDletextendsMIDlet{
privatePopUpTextBoxCanvascanvas;
protectedStringcanvasText="TextfromtheTextBox";
publicvoidstartApp(){
canvas=newPopUpTextBoxCanvas(this);
Display.getDisplay(this).setCurrent(canvas);
}
publicvoidpauseApp(){
}
publicvoiddestroyApp(booleanunconditional){
}
protectedvoidcloseTextBox(booleanupdate){
if(update)canvasText=canvas.textbox.getString();
if(canvas.textbox!=null)canvas.textbox=null;
Display.getDisplay(this).setCurrent(canvas);
}
protectedvoidshowError(Stringtitle,Stringtext){
Alertalert=newAlert(title,text,null,AlertType.ERROR);
alert.setTimeout(Alert.FOREVER);
alert.getType().playSound(Display.getDisplay(this));
Displayablecurrent=Display.getDisplay(this).getCurrent();
if(currentinstanceofAlert){}
elseDisplay.getDisplay(this).setCurrent(alert);
}
}
[edit]
Sourcecode:PopUpTextBox.java
importjavax.microedition.lcdui.*;
publicclassPopUpTextBoxextendsTextBoximplementsCommandListener{
privateCommandokCommand;
privateCommandcancelCommand;
privatePopUpTextBoxMIDletmidlet;
publicPopUpTextBox(Stringtitle,Stringtext,intmaxsize,intconstraints,PopUpTextBoxMIDletmidlet){
super(title,text,maxsize,constraints);
this.midlet=midlet;
okCommand=newCommand("Ok",Command.OK,1);
cancelCommand=newCommand("Cancel",Command.CANCEL,1);
this.addCommand(okCommand);
this.addCommand(cancelCommand);
this.setCommandListener(this);
}
publicvoidcommandAction(Commandc,Displayabled){
if(c==okCommand){
midlet.closeTextBox(true);
}
if(c==cancelCommand){
midlet.closeTextBox(false);
}
}
}
[edit]
Sourcecode:PopUpTextBoxCanvas.java
importjavax.microedition.lcdui.Canvas;
importjavax.microedition.lcdui.Command;
importjavax.microedition.lcdui.CommandListener;
importjavax.microedition.lcdui.Display;
importjavax.microedition.lcdui.Displayable;
importjavax.microedition.lcdui.Graphics;
importjavax.microedition.lcdui.TextField;
publicclassPopUpTextBoxCanvasextendsCanvasimplementsCommandListener{
privatePopUpTextBoxMIDletmidlet;
privateCommandenterCommand;
privateCommandexitCommand;
protectedPopUpTextBoxtextbox;
privateintwidth;
privateintheight;
publicPopUpTextBoxCanvas(PopUpTextBoxMIDletmidlet){
this.midlet=midlet;
enterCommand=newCommand("Entertext",Command.SCREEN,1);
exitCommand=newCommand("Exit",Command.EXIT,1);
this.addCommand(enterCommand);
this.addCommand(exitCommand);
this.setCommandListener(this);
}
publicvoidpaint(Graphicsg){
g.setColor(255,255,255);
g.fillRect(0,0,width,height);
g.setColor(0,0,0);
g.drawString(midlet.canvasText,0,0,Graphics.TOP|Graphics.LEFT);
}
protectedvoidkeyPressed(intkeyCode){}
protectedvoidkeyReleased(intkeyCode){}
protectedvoidkeyRepeated(intkeyCode){}
protectedvoidpointerDragged(intx,inty){}
protectedvoidpointerPressed(intx,inty){}
protectedvoidpointerReleased(intx,inty){}
protectedvoidsizeChanged(intw,inth){
width=w;
height=h;
repaint();
}
publicvoidcommandAction(Commandc,Displayabled){
if(c==enterCommand){
textbox=newPopUpTextBox("Entertext",midlet.canvasText,1000,TextField.ANY,midlet);
Display.getDisplay(midlet).setCurrent(textbox);
}
if(c==exitCommand){
midlet.notifyDestroyed();
}
}
}
download http://www.forum.nokia.com/piazza/wiki/images/b/b3/PopUpTextBoxMIDlet.zip