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

📄 listdemo.java

📁 图书中的经典例子 好好看看 会有帮助的。java2me版本的
💻 JAVA
字号:
//listDemo.java file
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;

public class listDemo extends MIDlet implements CommandListener
{
    private Command exitCommand, commandEx, commandMul, aCommand1, aCommand2;
    private List lbEx, lbMul;
    private Alert alert1;
    private String[] stringArray;
    public listDemo()
    {
        String[] stringArray = {"Item 1","Item 2","Item 3"};
        exitCommand =new Command("Exit",Command.EXIT,1);
        //转换到多选模式列表框的命令
        commandMul =new Command("switch2Multiple",Command.SCREEN,1);
        //显示单选列表框选择结果的命令
        aCommand1 =new Command("showSelected",Command.SCREEN,2);
        //转换到单选模式列表框的命令
        commandEx =new Command("switch2Exclusive",Command.SCREEN,1);
        //显示多选列表框选择结果的命令
        aCommand2 =new Command("showSelected",Command.SCREEN,2);
        //创建单选模式的列表框
        lbEx = new List("ExclusiveList", List.EXCLUSIVE, stringArray, null);
        lbEx.addCommand(exitCommand);
        lbEx.addCommand(commandMul);
        lbEx.addCommand(aCommand1);
        lbEx.setCommandListener(this);
        //创建多选模式的列表框
        lbMul = new List("MultipleList", List.MULTIPLE, stringArray, null);
        lbMul.addCommand(exitCommand);
        lbMul.addCommand(commandEx);
        lbMul.addCommand(aCommand2);
        lbMul.setCommandListener(this);
        //创建显示选择结果的Alert对象
        alert1 = new Alert("select info");
    }
    protected void startApp(  ) throws MIDletStateChangeException
    {
        Display.getDisplay(this).setCurrent(lbEx);
    }

    protected void pauseApp(  )
    {
    }

    protected void destroyApp( boolean p1 )
    {
    }

    public void commandAction(Command c,Displayable d)
    {
        if (c ==exitCommand)
        {
            destroyApp(false);
            notifyDestroyed();
        }
        else if(c ==aCommand1)
        {//显示单选框中被选中的项
            int sel = lbEx.getSelectedIndex();
            if(sel != -1)
            {
                String title = lbEx.getString(sel);
                alert1.setString("selected = " + sel +" " + title);
            }
            else
            {
                alert1.setString("当前没有选项被选中");
            }
            Display.getDisplay(this).setCurrent(alert1,lbEx);
        }
        else if(c ==aCommand2)
        {//显示复选框中被选中的项
            StringBuffer sb = new StringBuffer(30);
            boolean[] selFlags = {false,false,false};
            boolean selected = false;
            lbMul.getSelectedFlags(selFlags);
            for(int i=0;i<3;i++)
            {//检查每个选项是否被选中
                if( selFlags[i])
                {
                    sb.append("selected = " + i +" " +lbMul.getString(i)+ "\n\r");
                    selected = true;
                }
            }
            if(selected)
            {
                alert1.setString(sb.toString());
            }
            else
            {
                alert1.setString("当前没有选项被选中");
            }
            selFlags = null;
            sb=null;
            Display.getDisplay(this).setCurrent(alert1,lbMul);
        }
        else if(c ==commandEx)
        {//显示单选列表框
            Display.getDisplay(this).setCurrent(lbEx);
        }
        else if(c ==commandMul)
        {//显示复选列表框
            Display.getDisplay(this).setCurrent(lbMul);
        }
    }
}

⌨️ 快捷键说明

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