import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
//TextBox事例,made by sword2008
public class TextBoxDemo extends MIDlet implements CommandListener
{
//private
private Display display;
private ChoiceGroup types;
private ChoiceGroup options;
private Form mainform;
private Command exitcmd=new Command("EXIT",Command.EXIT,1);
private Command backcmd=new Command("BACK",Command.BACK,1);
private Command screencmd=new Command("SCREEN",Command.SCREEN,1);
private boolean firstTime;
//label
String textlabels[]={"字","邮箱","NUMBRIC","DECIMAL","电话","网址",};
int textBoxType[]={TextField.ANY,TextField.EMAILADDR,TextField.NUMERIC,
TextField.DECIMAL,TextField.PHONENUMBER,TextField.URL};
public TextBoxDemo(){
display=Display.getDisplay(this);
firstTime=true;
}
public void startApp(){
if(firstTime)
{mainform=new Form("选择一种textBox类型");
mainform.append("以下为选项:");
types=new ChoiceGroup("选择",Choice.EXCLUSIVE,textlabels,null);
mainform.append(types);
//第二选项
String optionsString[]={"密码形式","移动字符"};
options=new ChoiceGroup("选择",Choice.MULTIPLE,optionsString,null);
mainform.append(options);
mainform.addCommand(screencmd);
mainform.addCommand(exitcmd);
mainform.setCommandListener(this);
firstTime=false;
}
display.setCurrent(mainform);
}
public void pauseApp(){}
//按键监听
public void commandAction(Command cmd,Displayable dis)
{if(cmd==exitcmd){
destroyApp(false);
notifyDestroyed();
}
else if(cmd==screencmd){
int index=types.getSelectedIndex();
String title=textlabels[index];
int choiceType=textBoxType[index];
boolean flag[]=new boolean[2];
options.getSelectedFlags(flag);
if(flag[0]){
choiceType |=TextField.PASSWORD;}
TextBox textBox=new TextBox(title,"",50,choiceType);
if(flag[1]){
textBox.setTicker(new Ticker("TextBox"+title));}
textBox.addCommand(backcmd);
textBox.setCommandListener(this);
display.setCurrent(textBox);
}
else if(cmd==backcmd){}
}
public void destroyApp(boolean unkowmn){}
}
/*注意:如果出现的错误:
Project settings saved
Building "TextBoxDemo"
f:\WTK22\apps\TextBoxDemo\src\TextBoxDemo.java:5: TextBoxDemo is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable) in javax.microedition.lcdui.CommandListener
public class TextBoxDemo extends MIDlet implements CommandListener
^
1 error
com.sun.kvem.ktools.ExecutionException
Build failed
一定是CommandAction出现问题,检查一下两个形式参数(Command cmd,Displayable dis)
注意是Displayable而不是Display
*/
评论