1、仅在ITEM类实例被激活时,命令才会出现
2、命令不用addCommand加入表单中。
3、图
4、代码
package test;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class VisualMIDlet extends MIDlet implementsCommandListener,ItemCommandListener {
privateboolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc="Generated Fields ">
private Formform;
privateTextField textField;
privateAlert alert;
privateCommand exitCommand;
//</editor-fold>
private StringItem link=null;
privateCommandlinkCommand;
publicVisualMIDlet() {
}
//<editor-fold defaultstate="collapsed" desc="Generated Methods ">
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: initialize ">
private voidinitialize() {
// write pre-initialize user code here
// write post-initialize user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: startMIDlet ">
public voidstartMIDlet() {
// write pre-action user code here
switchDisplayable(null, getForm());
// writepost-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: resumeMIDlet ">
public voidresumeMIDlet() {
// write pre-action user code here
switchDisplayable(null, getForm());
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: switchDisplayable ">
public voidswitchDisplayable(Alert alert, Displayable nextDisplayable) {
// write pre-switch user code here
Display display = getDisplay();
if (alert == null) {
display.setCurrent(nextDisplayable);
} else {
display.setCurrent(alert, nextDisplayable);
}
// write post-switch user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: form ">
public FormgetForm() {
if (form == null) {
// write pre-init user code here
form = new Form("form", new Item[] { getTextField() });
form.addCommand(getExitCommand());
form.setCommandListener(this);
form.append("访问更多精彩内容");
form.append(getLink());
// write post-init user code here
}
return form;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: commandAction for Displayables">
public voidcommandAction(Command command, Displayable displayable) {
// write pre-action user code here
if (displayable == form) {
if (command == exitCommand) {
// write pre-action user code here
exitMIDlet();
// write post-action user code here
}
}
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: exitCommand ">
publicCommand getExitCommand() {
if (exitCommand == null) {
// write pre-init user code here
exitCommand = new Command("/u9000/u51FA", Command.EXIT, 0);
// write post-init user code here
}
return exitCommand;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: alert ">
public AlertgetAlert() {
if (alert == null) {
// write pre-init user code here
alert = new Alert("/u606D/u559C","/u60A8/u9009/u62E9/u4E86/u67E5/u770B/u66F4/u591A/u5185/u5BB9",null, AlertType.INFO);
alert.setCommandListener(this);
alert.setTimeout(Alert.FOREVER);
// write post-init user code here
}
return alert;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: textField ">
publicTextField getTextField() {
if (textField == null) {
// write pre-init user code here
textField = new TextField("textField", null, 32,TextField.ANY);
// write post-init user code here
}
return textField;
}
//</editor-fold>
publicvoid getLinkCommand() {
if (linkCommand == null) {
// write pre-init user code here
linkCommand = new Command("访问", Command.ITEM, 0);
// write post-init user code here
}
}
public StringItem getLink(){
if (link == null) {
// write pre-init user code here
getLinkCommand();
link = new StringItem(null,"点击这里",Item.HYPERLINK);
link.setDefaultCommand(linkCommand);
link.setItemCommandListener(this);
// write post-init user code here
}
return link;
}
//</editor-fold>
publicDisplay getDisplay () {
return Display.getDisplay(this);
}
public voidexitMIDlet() {
switchDisplayable (null, null);
destroyApp(true);
notifyDestroyed();
}
public voidstartApp() {
if (midletPaused) {
resumeMIDlet ();
} else {
initialize ();
startMIDlet ();
}
midletPaused = false;
}
public voidpauseApp() {
midletPaused = true;
}
public voiddestroyApp(boolean unconditional) {
}
public void commandAction(Commandcommand, Item item) {
if (command==linkCommand){
switchDisplayable(getAlert(),getForm());
}
}
}