J2ME List_Implicit

阅读更多
import javax.microedition.lcdui.Choice;
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;

//简易List 测试
public class List_Implicit_test extends MIDlet implements CommandListener {

	private Display display;
	public List_Implicit_test(){
		super();
	}
	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub

	}

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

	}

	protected void startApp() throws MIDletStateChangeException {
		// TODO Auto-generated method stub
		// 获取对象
		display = Display.getDisplay(this);

		//创建一个隐含模式(Implicit)  的list
		List list = new List("Implicit list测试",Choice.IMPLICIT);
		list.append("开始游戏", null);
		list.append("设置游戏", null);
		list.append("退出游戏", null);

		//设置默认选项
		list.setSelectedIndex(0, true);

		//添加一个命令
		list.addCommand(new Command("确定",Command.OK,1));

		//设置命令
		list.setCommandListener(this);

		display.setCurrent(list);
	}

	public void commandAction(Command c, Displayable s) {
		// TODO Auto-generated method stub
		//在隐含模式中:List 会在用户选择之后立刻引发事件,并将List.SELECT_COMMAND 作为参数第一个传入
		//如果不希望作为第一个参数传入可以设setSelectCommand(null) 关掉

		//
		if(c==List.SELECT_COMMAND){
			List temp = (List)s;
			int selected = temp.getSelectedIndex();
			System.out.println("第"+selected+"个选项被选中");
		}else{
			//获取操作对应 标签的名称
			String label = c.getLabel();
			if(label.equals("确定")){
				List temp = (List)s;
				int i = temp.getSelectedIndex();
				String info = temp.getString(i);
				System.out.println("第"+(i+1)+"个的信息为"+info);
			}
		}


	}

}

你可能感兴趣的:(j2me)