日期:2014-05-20 浏览次数:20672 次
StringItem是一个文本控件,除了可以显示文本在屏幕上以外,还可以作为文字按钮(需要绑定指定按钮)或连接控件,构造方法:item = new StringItem("Hyper-Link","http://hi.baidu.com/9prior",Item.HYPERLINK);
处理时间用ItemCommandListener()做监听器,用CommandAction(Command ,Item)来处理,以下给出一个文字连接网络程序,点击确定后将会转向连接网络的页面:
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import javax.microedition.lcdui.*;
public class stringItemDemo extends MIDlet implements CommandListener,ItemCommandListener {
private Display display;
private Form mainForm;
private Command httpCommand = new Command("Go",Command.ITEM,1); //item按钮类型是提示实现屏幕上某个功能
private Command okCommand = new Command("ok",Command.ITEM,1);
private Command exitCommand = new Command("Exit",Command.ITEM,1);
public stringItemDemo() {
?? mainForm = new Form("String Item Demo");
????????????????????????????????????????? //创建要添加到mainForm中的StringItem对象
?? StringItem item = new StringItem("标题:","StringItem具体内容");
?? mainForm.append(item);
????????????????????????????????????????? //超级链接类型的Item
?? item = new StringItem("Hyper-Link","http://hi.baidu.com/9prior",Item.HYPERLINK);
?? item.setDefaultCommand(httpCommand); //设置了屏幕上要显示的按钮是httpCommand,等于绑定在其按钮上了,功能在此按钮实现
?? item.setItemCommandListener(this); //设置了监听器
?? mainForm.append(item);
???????????????????????????????????????? //按钮类型
?? item = new StringItem("","确定",Item.BUTTON);
?? item.setDefaultCommand(okCommand); //设置屏幕上要显示的按钮是okCommand
?? item.setItemCommandListener(this);
?? mainForm.append(item);
??
?? mainForm.addCommand(exitCommand);
?? mainForm.setCommandListener(this);
}
//处理控件事件
protected void startApp() throws MIDletStateChangeException {
?? display = Display.getDisplay(this);
?? display.setCurrent(mainForm);
??
}
public void commandAction(Command c, Displayable d) {
?? if (c == exitCommand)
?? {
??? try {
???? destroyApp(false);
??? } catch (MIDletStateChangeException e) {
???? // TODO Auto-generated catch block
???? e.printStackTrace();
??? }
??? notifyDestroyed();
?? }
??
??
}
public void commandAction(Command c, Item item) {
??
?? if (c == httpCommand)
?? {
??? String text = "正在连接网络.....";
??? //Alert a = new Alert("URL",text,null,AlertType.INFO);
??? TextBox tb = new TextBox("Alert",text,15,0);
??? display.setCurrent(tb); //这一步使得当前屏幕转成了tb
?? }
?? else
??? if(c == okCommand)
??? {
???? String text = "你单击了【确定】...";
???? TextBox tb1 = new TextBox("Alert",text,15,0);
???? display.setCurrent(tb1);
??? }
??
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
??
}
protected void pauseApp() {
??
}
}
?
?
?