本文是关于J2ME的StringItem中实现局部按钮触发功能。参考程序代码: package fuhuaking.StringItem; import javax.microedition.lcdui.Alert; import javax.microedition.lcdui.AlertType; 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.Item; import javax.microedition.lcdui.ItemCommandListener; import javax.microedition.lcdui.StringItem; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; /** * @author li.fuwa * 2010-1-6 */ public class StringItemButton extends MIDlet implements ItemCommandListener, CommandListener { private Display display; private Form form; private StringItem string1; private StringItem string2; private Command button1_cmd; private Command exit_cmd; private Alert alert; public StringItemButton() { display = Display.getDisplay(this); form = new Form("StringItemButton"); string1 = new StringItem("1", "file1"); string2 = new StringItem("2", "file2"); button1_cmd = new Command("Button", Command.CANCEL, 1); exit_cmd = new Command("Exit", Command.EXIT, 1); alert = new Alert("Warning", "Button enter", null, AlertType.WARNING); form.append(string1); form.append(string2); form.addCommand(exit_cmd); string2.setDefaultCommand(button1_cmd); string2.setItemCommandListener(this); alert.setTimeout(5000); form.setCommandListener(this); display.setCurrent(form); } protected void destroyApp(boolean arg0) throws MIDletStateChangeException { } protected void pauseApp() { } protected void startApp() throws MIDletStateChangeException { } public void commandAction(Command cmd, Item item) { if (cmd == button1_cmd && item == string2) { display.setCurrent(alert); } } public void commandAction(Command cmd, Displayable arg1) { if (cmd == exit_cmd) { try { destroyApp(true); } catch (MIDletStateChangeException e) { e.printStackTrace(); } notifyDestroyed(); } } }