import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.Form; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** * textbox的构造方法为:TextBox(String 标题,String 初始内容,int 最多字数,int 类型). */ public class textboxTest extends MIDlet implements CommandListener{ private Display display; private Form form ; private Command cmd1; private Command cmd2; private TextBox tb; //构造方法 public textboxTest() { display = Display.getDisplay(this); System.out.println("这里初始化"); } //开始运行 protected void startApp() throws MIDletStateChangeException { //System.out.println("这里开始了程序!"); cmd1 = new Command("确定",Command.BACK,1); cmd2 = new Command("返回",Command.BACK,1); tb = new TextBox("我的宝贝", "xiaopai", 30, TextField.ANY); tb.addCommand(cmd1); tb.setCommandListener(this); form = new Form("paipai"); form.addCommand(cmd2); form.setCommandListener(this); display.setCurrent(tb); } //接口方法 public void commandAction(Command cmd, Displayable dpa) { if(cmd==cmd1){ String str = tb.getString(); form.append(str); display.setCurrent(form); }else{ display.setCurrent(tb); } //System.out.println("命令按钮单击事件!"); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { System.out.println("程序退出!"); } protected void pauseApp() { System.out.println("程序暂停!"); } }