以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  程序代码——ChoiceGroup  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=544)

--  作者:admin
--  发布时间:2008/9/25 10:10:28
--  程序代码——ChoiceGroup

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;

public class Exec extends MIDlet implements CommandListener {
        Display display;
        Form form = new Form("表单");
        String[] orderDishes = { "狮子头", "宫保鸡丁", "青椒牛柳" };
        String[] quantities = { "1份", "2份", "4份", "8份" };
        ChoiceGroup choice1 = new ChoiceGroup("菜名", Choice.MULTIPLE, orderDishes,
                        null);
        ChoiceGroup choice2 = new ChoiceGroup("菜名", Choice.EXCLUSIVE, quantities,
                        null);
        Command cmd1 = new Command("显示", Command.ITEM, 1);
        Command cmd2 = new Command("退出", Command.EXIT, 1);
        Alert alert = new Alert("结果");

        public void startApp() {
                display = Display.getDisplay(this);
                form.append("订菜");
                form.append(choice1);
                form.append(choice2);
                form.addCommand(cmd1);
                form.addCommand(cmd2);
                form.setCommandListener(this);
                display.setCurrent(form);
        }

        public void pauseApp() {
                display.setCurrent(null);
        }

        public void destroyApp(boolean unconditional) {

        }

        public void commandAction(Command c, Displayable arg1) {
                if (c == cmd1) {
                        StringBuffer result = new StringBuffer("你选择的订餐有");
                        for (int i = 0; i < choice1.size(); i++) {
                                if (choice1.isSelected(i))
                                        result
                                                        .append(((i == 0) ? ":" : ",")
                                                                        + choice1.getString(i));
                        }
                        result.append("\\n份数为:"
                                        + choice2.getString(choice2.getSelectedIndex()));
                        alert.setString(result.toString());
                        alert.setTimeout(2000);
                        display.setCurrent(alert);
                } else if (c == cmd2) {
                        destroyApp(false);
                        notifyDestroyed();
                }
        }
}

[此贴子已经被作者于2010-12-12 18:31:46编辑过]