⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 selectmultiplelist.java

📁 用Eclipse开发J2me书籍的程序源码。
💻 JAVA
字号:
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class SelectMultipleList extends MIDlet implements CommandListener {
    private Display display;
    
    //定义List菜单的内容
    private static String menuItems[] = {"List Item1",
            "List Item2",
            "List Item3",
            "List Item4",
            "List Item5"
    };

    private List lstMultiple = new List(
            "Multiple List",                    //标题
            List.MULTIPLE,                      //隐式类型 
            menuItems,                          //设置列表内容   
            null);                              //列表不使用图标
    
    //用于读取选中的选项
    private Command cmdRead = new Command("Read", Command.SCREEN, 1);
    //用于设置被选中的选项
    private Command cmdSet = new Command("Set", Command.SCREEN, 1);
    //全部选中
    private Command cmdSelectAll = new Command("Select All", 
            Command.SCREEN, 1);
    //Exit
    private Command cmdExit = new Command("Exit", Command.EXIT, 1);
    
    public SelectMultipleList() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void startApp() throws MIDletStateChangeException {
        //设置命令监听
        lstMultiple.addCommand(cmdExit);
        lstMultiple.addCommand(cmdRead);
        lstMultiple.addCommand(cmdSet);
        lstMultiple.addCommand(cmdSelectAll);        
        lstMultiple.setCommandListener(this);
        
        //获得当前MIDlet的Display对象
        display = Display.getDisplay(this); 
        //设置List对象为当前显示对象
        display.setCurrent(lstMultiple);  
    }

    protected void pauseApp() {
        // TODO Auto-generated method stub

    }

    protected void destroyApp(boolean arg0) 
        throws MIDletStateChangeException {
        // TODO Auto-generated method stub

    }

    /**
     * 处理命令按钮事件
     */
    public void commandAction(Command c, Displayable d) {
        if (c == this.cmdRead) {
            //读取选中的选项 - 方法1
            System.out.println("读取选中的选项 - 方法1输出内容");
            for (int i=0; i< lstMultiple.size(); i++) {
                if (lstMultiple.isSelected(i)) {
                    String str = lstMultiple.getString(i);
                    System.out.println("Item: '" + str + "' is selected");
                }
            }
            
            //读取选中的选项 - 方法2
            System.out.println("\n\n读取选中的选项 - 方法2输出内容");
            boolean flags[] = new boolean[lstMultiple.size()];
            lstMultiple.getSelectedFlags(flags);
            for (int i=0; i< flags.length; i++) {
                if (flags[i]) {
                    String str = lstMultiple.getString(i);
                    System.out.println("Item: '" + str + "' is selected");
                }
            }
        } else if (c == this.cmdSet) {
            //设置选中的内容 - 方法1
            lstMultiple.setSelectedIndex(0, true);
            lstMultiple.setSelectedIndex(1, true);
            lstMultiple.setSelectedIndex(2, true);
            lstMultiple.setSelectedIndex(3, false);
            lstMultiple.setSelectedIndex(4, false);
            
            //设置选中的内容 - 方法2
            //boolean flags[] = {true, true, true, false, false};
            //lstMultiple.setSelectedFlags(flags);
        } else if (c == this.cmdSelectAll) {
            //全选,下面的算法在选项很多时或者无法预先知道选项个数时有意义
            boolean flags[] = new boolean[lstMultiple.size()];
            for (int i=0;i<flags.length;i++) {
                flags[i] = true;
            }
            lstMultiple.setSelectedFlags(flags);
        } else if (c == this.cmdExit) {
            notifyDestroyed();
        }
    }    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -