MIDP高级UI的使用(三)TextBox
时间:2009-10-10 博客园 农民学编程
当我们需要再移动设备上输入数据时,TextBox 就派上用场了,我们使用的TextBox 构造函数参数共有四个,TextBox textbox = new TextBox(string title, string content, string maxLength, string limitType) ,第一个是标题,第二个是TextBox 的初始内容,第三个是允许输入字符的最大长度,第四个是限制内型。值得注意的是:一个TextBox 必须附加一个命令,否则用户将不能激发任何行为,而陷入这个TextBox 中。
下面是一个常见的TextBox 的例子。
view plainprint?
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.thinkrace.TextBox;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
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.lcdui.Ticker;
import javax.microedition.midlet.*;
/**
* @author pengjw
*/
public class TextBoxDemo extends MIDlet implements CommandListener{
private Display display;
private ChoiceGroup types;
private ChoiceGroup options;
private Form mainForm;
private static final Command CMD_EXIT = new Command("Exit", Command.EXIT, 1);
private static final Command CMD_BACK = new Command("Back", Command.BACK, 1);
private static final Command CMD_SHOW = new Command("Show", Command.SCREEN ,1);
private static final String[] textBoxLabels ={
"Any Character", "Email", "Number", "Decimal", "Phone", "URL"
};
private static final int[] textBoxTypes ={
TextField.ANY, TextField.EMAILADDR, TextField.NUMERIC,
TextField.DECIMAL, TextField.PHONENUMBER, TextField.URL
};
//firstTime用来判断TextBoxDemo是否被实例化
private boolean firstTime;
public TextBoxDemo(){
/**
* 来自文档的说明
* Display represents the manager of the display and input devices of the system.
* It includes methods for retrieving properties of the device and for requesting that objects be displayed on the device.
* There is exactly one instance of Display per MIDlet and the application can get
* a reference to that instance by calling the getDisplay() method.
*/
display = Display.getDisplay(this);
firstTime = true;
}
public void startApp() {
if(firstTime){
mainForm = new Form("Select a Text Box Type");
mainForm.append("select a textbox type");
types = new ChoiceGroup("Ch
|