MULTIPLEItemStateListenerDemo.java
上传用户:gyyuli
上传日期:2013-07-09
资源大小:3050k
文件大小:2k
源码类别:

J2ME

开发平台:

Java

  1. import javax.microedition.lcdui.*;
  2. import javax.microedition.midlet.MIDlet;
  3. public class MULTIPLEItemStateListenerDemo
  4.     extends MIDlet implements CommandListener {
  5.     private final static Command CMD_EXIT = new Command("Exit", Command.EXIT, 
  6.                                                         1);        
  7.     private Display display;
  8.     private ChoiceGroup mChoiceGroup;
  9.     private Form mainForm;
  10.     private StringItem mStringItem;
  11.     public MULTIPLEItemStateListenerDemo() {
  12.        display = Display.getDisplay(this);
  13.        mainForm = new Form("Choice Group演示");
  14.        Image[] imageArray = null;
  15.        try {
  16.                 Image star = Image.createImage("/star.png");
  17.                 imageArray = new Image[] { star, star, star, star, star, star};
  18.        } catch (java.io.IOException err) {
  19.        }
  20.        String[] stringArray = {
  21.                 "红心大战","空当接龙","三维弹球","蜘蛛纸牌","翻转棋","扫雷"
  22.             };
  23.        mChoiceGroup = new ChoiceGroup("选择游戏项目",ChoiceGroup.MULTIPLE,stringArray,imageArray);
  24.        mainForm.append(mChoiceGroup);
  25.        mainForm.addCommand(CMD_EXIT);
  26.        mainForm.setCommandListener(this);
  27.        
  28.        mStringItem = new StringItem("您选择的是","——");
  29.        mainForm.append(mStringItem);
  30.        ItemStateListener listener = new ItemStateListener(){
  31.        public void itemStateChanged(Item item){
  32.        if(item == mChoiceGroup){
  33.        boolean[] selectedArray = new boolean[mChoiceGroup.size()];
  34.        mChoiceGroup.getSelectedFlags(selectedArray);
  35.        StringBuffer selectedString = new StringBuffer();
  36.        for(int i=0;i<selectedArray.length;i++){
  37.        if(selectedArray[i]){
  38.        selectedString.append(" ").append(new StringBuffer(mChoiceGroup.getString(i)));
  39.        }
  40.        }
  41.        mStringItem.setText(selectedString.toString());
  42.        }
  43.        }
  44.        };
  45.        mainForm.setItemStateListener(listener); 
  46.     }
  47.     protected void startApp() {        
  48.         display.setCurrent(mainForm);
  49.     }
  50.     public void commandAction(Command c, Displayable d) {
  51.         if (c == CMD_EXIT) {
  52.             destroyApp(false);
  53.             notifyDestroyed();
  54.         }
  55.     }
  56.     protected void destroyApp(boolean unconditional) {
  57.     }
  58.     protected void pauseApp() {
  59.     }
  60. }