一个list 例子来看,demo 中的例子,
打开toolkid2.52中,创建listDemo工程文件,然后创建listDeom类,
把下面代码贴在src下面的新建立的listdemo.java文件名中,在toolkid
中build,run,可以看到实际运行结果。
说明下:
1.import 导入lcdui 类包。这是一个界面包。
2.Display 类,display 实例,通过setCurrent切换到当前界面。
3.commandAction函数,处理相应按钮事件。
************************************************************
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
public class ListDemo extends MIDlet implements CommandListener {
private static final Command CMD_EXIT = new Command("退出", Command.EXIT, 1);
private static final Command CMD_BACK = new Command("返回", Command.BACK, 1);
private Display display;
private List mainList;
private List exclusiveList;
private List implicitList;
private List multipleList;
private boolean firstTime;
public ListDemo() {
display = Display.getDisplay(this);
String[] stringArray = { "Option A", "Option B", "Option C", "Option D" };
Image[] imageArray = null;
//创建单选list 界面,包括退出按钮和返回按钮及其事件
exclusiveList = new List("单选", Choice.EXCLUSIVE, stringArray, imageArray);
exclusiveList.addCommand(CMD_BACK);
exclusiveList.addCommand(CMD_EXIT);
exclusiveList.setCommandListener(this);
//
implicitList = new List("简单", Choice.IMPLICIT, stringArray, imageArray);
implicitList.addCommand(CMD_BACK);
implicitList.addCommand(CMD_EXIT);
implicitList.setCommandListener(this);
//多选list
multipleList = new List("多选", Choice.MULTIPLE, stringArray, imageArray);
multipleList.addCommand(CMD_BACK);
multipleList.addCommand(CMD_EXIT);
multipleList.setCommandListener(this);
firstTime = true;
}
protected void startApp() {
if (firstTime) {
Image[] imageArray = null;
try {
// 加载icon,位置在res下相对目录
Image icon = Image.createImage("/midp/uidemo/Icon.png");
imageArray = new Image[] { icon, icon, icon };
} catch (java.io.IOException err) {
}
String[] stringArray = { "单选", "简单", "多选" };
//创建界面
mainList = new List("Choose type", Choice.IMPLICIT, stringArray, imageArray);
mainList.addCommand(CMD_EXIT);
mainList.setCommandListener(this);
//设置界面为当前为mainList
display.setCurrent(mainList);
firstTime = false;
}
}
protected void destroyApp(boolean unconditional) {
}
protected void pauseApp() {
}
//处理按钮事件
public void commandAction(Command c, Displayable d) {
if (d.equals(mainList)) { //当前界面为mainlist
if (c == List.SELECT_COMMAND) { //
if (d.equals(mainList)) {
//置换界面
switch (((List)d).getSelectedIndex())
{
case 0:
display.setCurrent(exclusiveList);
break;
case 1:
display.setCurrent(implicitList);
break;
case 2:
display.setCurrent(multipleList);
break;
}
}
}
} else {
//在子界面,按钮为CMD_BACK,返回主list界面
if (c == CMD_BACK) {
display.setCurrent(mainList);
}
}
if (c == CMD_EXIT) {
destroyApp(false);
notifyDestroyed();
}
}
}