J2ME中的ChoiceGroup类可以实现单选和多选,同时,它的类型又可以分为显式、隐式或者弹出式
下边的代码说明了如何在J2ME程序中使用不同类型的ChoiceGroups.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class ChoiceMidlet extends MIDlet implements CommandListener {
Command exit = new Command("Exit", Command.EXIT, 1);
private Display display;
private boolean firsttime;
private Form form;
public ChoiceMidlet() {}
protected void startApp() {
display = Display.getDisplay(this);
form = new Form("Choice Midlet");
String[] stringArray = {"Choice A", "Choice B", "Choice C"};
ChoiceGroup[] groups = {
new ChoiceGroup("Exclusive",
ChoiceGroup.EXCLUSIVE, stringArray,null),
new ChoiceGroup("Multiple",
ChoiceGroup.MULTIPLE, stringArray,null),
new ChoiceGroup("Pop-Up",
ChoiceGroup.POPUP, stringArray,null)
};
for (int i = 0; i < groups.length; i++) {
form.append(groups[i]);
}
form.addCommand(exit);
form.setCommandListener(this);
display.setCurrent(form);
}
public void commandAction(Command c, Displayable d) {
if (c == exit) {
destroyApp(false);
notifyDestroyed();
}
}
protected void destroyApp(boolean unconditional) {}
protected void pauseApp() {}
}