|
|
选择菜单是j2me重要的板块,它给我们提供了动态交互信息的窗口,选择菜单有复选、单选、隐含式单选,下面的例子式综合三者的例子,有一定的代表性。。。
import javax.microedition.midlet.MIDlet;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Choice;
public class ComplexListExample extends MIDlet implements CommandListener {
private static final Command exitCommand = new Command("退出", Command.EXIT, 1);
private static final Command backCommand = new Command("返回", Command.BACK, 2);
private Display display;
private List mainList, exclusiveList, mutipleList, implicitList;
private boolean bFirstTime;
public ComplexListExample() {
display = Display.getDisplay(this);
}
protected void startApp() {
init();
if(bFirstTime) {
String[] stringArray = {
"Exclusive",
"Implicit",
"Multiple"
};
mainList = new List("Choose type", Choice.IMPLICIT, stringArray, null);
mainList.addCommand(exitCommand);
mainList.setCommandListener(this);
display.setCurrent(mainList);
bFirstTime = false;
}
}
protected void pauseApp() {
}
protected void destroyApp(boolean unconditional) {
}
public void commandAction(Command c, Displayable d) {
if(d.equals(mainList)) {
if (c == List.SELECT_COMMAND) {
switch (((List) d).getSelectedIndex()) {
case 0:
display.setCurrent(exclusiveList);
break;
case 1:
display.setCurrent(implicitList);
break;
case 2:
display.setCurrent(mutipleList);
break;
}
}
} else {
if(c == backCommand) {
display.setCurrent(mainList);
}
}
if(c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
}
public void init() {
String[] elements = {"one", "two", "three", "four"};
exclusiveList = new List("mainList", Choice.EXCLUSIVE, elements, null);
exclusiveList.addCommand(exitCommand);
exclusiveList.addCommand(backCommand);
exclusiveList.setCommandListener(this);
mutipleList = new List("mainList", Choice.MULTIPLE, elements, null);
mutipleList.addCommand(exitCommand);
mutipleList.addCommand(backCommand);
mutipleList.setCommandListener(this);
implicitList = new List("mainList", Choice.IMPLICIT, elements, null);
implicitList.addCommand(exitCommand);
implicitList.addCommand(backCommand);
implicitList.setCommandListener(this);
bFirstTime = true;
}
} |
|