课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——ChoiceGroup


  共有16004人关注过本帖树形打印复制链接

主题:程序代码——ChoiceGroup

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1949 积分:26803 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——ChoiceGroup  发帖心情 Post By:2008/9/25 10:10:28 [只看该作者]

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编辑过]

 回到顶部