J2ME-ChoiceGroup多选应用

一、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) {
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值