一、ChoiceGroup可通过getSelectedFlags返回选择框的选择值的数组,按选项的索引值顺序排列。
二、效果
三、代码
package hello;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class HelloMIDlet extends MIDlet implements CommandListener{
privateboolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc="Generated Fields ">
privateCommand exitCommand;
privateCommand okCommand;
private Formform;
privateChoiceGroup sportschoice;
//</editor-fold>
privateboolean blischoice[];
privateStringItem SIshowchoice[];
publicHelloMIDlet() {
}
//<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());
// write post-action user code here
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Method: resumeMIDlet ">
public voidresumeMIDlet() {
// write pre-action user code here
// 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>
//显示选择后的结果
private void showchoice(){
int choicedcount,choicelength,j;
j=0;
choicelength=sportschoice.size();
blischoice=new boolean[choicelength];
//getSelectedFlags返回选择框的选择值的数组,按选项的索引值顺序排列
choicedcount=sportschoice.getSelectedFlags(blischoice);
SIshowchoice=newStringItem[choicedcount];
for (int i=0;i<choicelength;i++){
if (j==choicedcount) break;
if (blischoice[i]) {
SIshowchoice[j]=newStringItem("",sportschoice.getString(i)+"/n");
form.append(SIshowchoice[j]);
j++;
}
}
if (j ==0 ) form.append(newStringItem("","您什么运动也不喜欢!"));
form.delete(0);
form.removeCommand(okCommand);
}
//<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
} else if (command == okCommand) {
// write pre-action user code here
showchoice();
// 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: form ">
public FormgetForm() {
if (form == null) {
// write pre-init user code here
form = new Form("/u4F60/u559C/u6B22/u4EC0/u4E48/u8FD0/u52A8", newItem[] { getSportschoice() });
form.addCommand(getExitCommand());
form.addCommand(getOkCommand());
form.setCommandListener(this);
// write post-init user code here
}
return form;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: sportschoice ">
publicChoiceGroup getSportschoice() {
if (sportschoice == null) {
// write pre-init user code here
sportschoice = new ChoiceGroup("/u8FD0/u52A8/u7C7B/u578B",Choice.MULTIPLE);
sportschoice.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
sportschoice.append("游泳", null);
sportschoice.append("打球",null);
sportschoice.append("跑步", null);
sportschoice.append("武术",null);
sportschoice.append("健身",null);
// write post-init user code here
}
return sportschoice;
}
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="Generated Getter: okCommand ">
publicCommand getOkCommand() {
if (okCommand == null) {
// write pre-init user code here
okCommand = new Command("/u786E/u5B9A", Command.OK, 0);
// write post-init user code here
}
return okCommand;
}
//</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) {
}
}