J2ME中使用TextField实现表单界面代码实战 收藏
本例主要借助Form和TextBox显示。
代码如下:
/**
* @作者 Jcuckoo
* @创建日期 2008-11-18
* @版本 V 1.0
*/
public class TextFieldMIDlet extends MIDlet implements CommandListener{
//声名
private Display display;
private Form myform;
private TextField userName;
private TextField userPwd;
private TextField email;
private TextField number;
private TextField value;
private TextField telnum;
private TextBox showMessage;
private Command saveCommand;
private Command backCommand;
private Command exitCommand;
public TextFieldMIDlet() {
display=Display.getDisplay(this);
myform=new Form("TextField实例测试");
userName=new TextField("姓名:","",20,TextField.ANY);
userPwd=new TextField("密码:","",20,TextField.PASSWORD);
email=new TextField("Email:","",20,TextField.EMAILADDR);
number=new TextField("数字字符串:","",20,TextField.NUMERIC);
value=new TextField("数值:","",20,TextField.DECIMAL);
telnum=new TextField("电话号码:","",20,TextField.PHONENUMBER);
saveCommand=new Command("保存",Command.SCREEN,1);
backCommand=new Command("返回",Command.BACK,1);
exitCommand=new Command("退出",Command.EXIT,1);
myform.append(userName);
myform.append(userPwd);
myform.append(email);
myform.append(number);
myform.append(value);
myform.append(telnum);
myform.addCommand(saveCommand);
myform.addCommand(exitCommand);
myform.setCommandListener(this);
showMessage=new TextBox("你输入的信息为:","",200,TextField.ANY);
showMessage.addCommand(backCommand);
showMessage.addCommand(exitCommand);
showMessage.setCommandListener(this);
}
protected void destroyApp(boolean arg0){
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(myform);
}
public void commandAction(Command c, Displayable d) {
if(c==saveCommand){
//方法一:使用String测试
/*
String tmp="";
tmp+=userName.getLabel()+userName.getString()+"\n";
tmp+=userPwd.getLabel()+userPwd.getString()+"\n";
tmp+=email.getLabel()+email.getString()+"\n";
tmp+=number.getLabel()+number.getString()+"\n";
tmp+=value.getLabel()+value.getString()+"\n";
tmp+=telnum.getLabel()+telnum.getString()+"\n";
showMessage.setString(tmp);
*/
//方法二:采用StringBuffer来实现,以提供资源利用率(建议使用此方法)。
StringBuffer bf=new StringBuffer();
bf.append(userName.getLabel());
bf.append(userName.getString());
bf.append("\n");
bf.append(userPwd.getLabel());
bf.append(userPwd.getString());
bf.append("\n");
bf.append(email.getLabel());
bf.append(email.getString());
bf.append("\n");
bf.append(number.getLabel());
bf.append(number.getString());
bf.append("\n");
bf.append(value.getLabel());
bf.append(value.getString());
bf.append("\n");
bf.append(telnum.getLabel());
bf.append(telnum.getString());
bf.append("\n");
showMessage.setString(new String(bf));
display.setCurrent(showMessage);
}
if(c==backCommand){
//返回清空表单
/*
userName.setString("");
userPwd.setString("");
email.setString("");
number.setString("");
value.setString("");
telnum.setString("");
*/
display.setCurrent(myform);
}
if(c==exitCommand){
destroyApp(false);
notifyDestroyed();
}
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/guoquanyou/archive/2008/11/18/3324016.aspx
评论