▼MIDP2.0メモ▼
チョイスグループを使う


チョイスグループを使うプログラムです。

 


プログラム
ChoiceGroupEx.java
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

//チョイスグループを使う(本体)
public class ChoiceGroupEx extends MIDlet {
    static MIDlet current;

    //コンストラクタ
    public ChoiceGroupEx(){
        current=this;
        (Display.getDisplay(this)).setCurrent(new ChoiceGroupForm());
    }

    //アプリの開始
    public void startApp() {
    }

    //アプリの一時停止
    public void pauseApp() {
    }

    //アプリの終了
    public void destroyApp(boolean unconditional) {
    }
}


ChoiceGroupForm.java
import javax.microedition.lcdui.*;

//チョイスグループを使う(フォーム)
class ChoiceGroupForm extends Form
    implements CommandListener {
    ChoiceGroup choExclusive;//チョイス-排他
    ChoiceGroup choMultiple; //チョイス-複合
    ChoiceGroup choPopup;    //チョイス-ポップアップ
    Command     soft1;       //ソフトキー1

    //コンストラクタ
    ChoiceGroupForm(){
        super("ChoiceGroupEx");

        //チョイス-排他
        choExclusive=new ChoiceGroup(
            "EXCLUSIVE",Choice.EXCLUSIVE);
        append(choExclusive);
        choExclusive.append("いちご",null);
        choExclusive.append("りんご",null);
        choExclusive.append("みんと",null);

        //チョイス-複合
        choMultiple=new ChoiceGroup(
            "MULTIPLE",Choice.MULTIPLE);
        append(choMultiple);
        choMultiple.append("ブラック",null);
        choMultiple.append("ホワイト",null);
        choMultiple.append("イエロー",null);

        //チョイス-ポップアップ
        choPopup=new ChoiceGroup(
            "POPUP",Choice.POPUP);
        append(choPopup);
        choPopup.append("三鷹",null);
        choPopup.append("四谷",null);
        choPopup.append("六本木",null);

        //ソフトキー1
        soft1=new Command("OK",Command.SCREEN,0);
        addCommand(soft1);
        setCommandListener(this);
    }

    //ソフトキーイベント
    public void commandAction(Command c,Displayable s) {
        //OK
        if (c==soft1) {
            //テキスト
            String text=choExclusive.getString(
                choExclusive.getSelectedIndex())+"と";
            for (int i=0;i<choMultiple.size();i++) {
                if (choMultiple.isSelected(i)) {
                    text+=choMultiple.getString((i))+"と";
                }
            }
            text+=choPopup.getString(choPopup.getSelectedIndex());
            text+="を選択しました。";
            
            //ダイアログ
            Alert alert=new Alert("情報",text,null,AlertType.INFO);
            alert.setTimeout(Alert.FOREVER);
            (Display.getDisplay(ChoiceGroupEx.current)).setCurrent(alert);
        }
    }
}



−戻る−