J2ME学习札记:Form和TextField的使用,附程序,截图(原创)
Form在这里就像之前说过的TextBox一样,作为一个画布,可以往里面添加很多控件对象并且显示出来,但Form可以存放更多的对象。而TextField就好比我们做页面时的表单输入框一样,提供给我们输入文字信息,其构造方法如下:TextField(标题,默认内容,最大系数,输入内容的类新),以下给出一个相关程序:
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
public class TextFieldDemo extends MIDlet implements CommandListener {
private Command exitCommand = new Command("Exit",Command.EXIT,1);
private Form mainForm;
public TextFieldDemo()
{
mainForm = new Form("From test");
}
protected void startApp() throws MIDletStateChangeException {
//开始添加各种种类的TextField对象
mainForm.append(new TextField("任何字符","",15,TextField.ANY)); //任何类型的
mainForm.append(new TextField("E-mail","",15,TextField.EMAILADDR));//E-mail类型的
mainForm.append(new TextField("数字","",15,TextField.NUMERIC));//整形数据类型
mainForm.append(new TextField("浮点数","",15,TextField.DECIMAL));//浮点类型
mainForm.append(new TextField("电话","",15,TextField.PHONENUMBER));//电话类型
mainForm.append(new TextField("密码","",15,TextField.PASSWORD));//密码类型
mainForm.append(new TextField("网站URL","",15,TextField.URL));//URL类型
mainForm.addCommand(exitCommand);
mainForm.setCommandListener(this);
Display.getDisplay(this).setCurrent(mainForm); //这样做可以显示Form屏幕
}
public void commandAction(Command c, Displayable d) {
if(c == exitCommand)
{
try {
destroyApp(false);
notifyDestroyed();
} catch (MIDletStateChangeException e) {
e.printStackTrace();
}
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}