import javax.microedition.lcdui.Alert;
import javax.microedition.
import javax.microedition.
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
public class MyMidlet extends MIDlet implements CommandListener {
List list;
Command comBack,comExit,comOk,comCancel;
Display display=null;
String []str=null;
Form form=null;
Alert alert;
public MyMidlet() {
str=new String[]{"打篮球","踢足球",
" 唱歌,跳舞等文艺活动",
"武术运动","其他"};
form=new Form("你的兴趣爱好是:");
list=new List("你的兴趣爱好:",List.MULTIPLE,str,null);
alert=new Alert("请选择你的爱好");
alert.setTimeout(Alert.FOREVER);
comBack=new Command("返回",Command.BACK,1);
comExit=new Command("退出",Command.EXIT,1);
comOk=new Command("确定",Command.OK ,1);
comCancel=new Command("取消",Command.CANCEL,1);
form.addCommand(comBack);
form.addCommand(comExit);
form.setCommandListener(this);
list.addCommand(comCancel);
list.addCommand(comOk);
list.addCommand(comExit);
list.setCommandListener(this);
}
protected void startApp() throws MIDletStateChangeException {
if(display==null){
display=Display.getDisplay(this);
display.setCurrent(list);
}
}
public void commandAction(Command c, Displayable d) {
if (c == comOk) {
// int i = list.getSelectedIndex();//单选才用此项
// 得到多选的boolean数组
boolean[] b = new boolean[list.size()];
list.getSelectedFlags(b);
//System.out.println(list.getSelectedFlags(b));
for (int i = 0; i < b.length; i++) {
if (b[i]) {// 若选中,则添加进form
String str1 = (String) list.getString(i);
form.append(str1);
}
}
display.setCurrent(form);
}
if(c==comCancel){
boolean []b=new boolean[list.size()];
list.setSelectedFlags(b);
//System.out.println(list.getSelectedFlags(b));
}
if(c==comBack){
form.deleteAll();
display.setCurrent(list);
}
if(c==comExit){
try {
destroyApp(false);
} catch (MIDletStateChangeException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
notifyDestroyed();
}
}
protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
}
protected void pauseApp() {
}
}
具体效果如下: