论坛首页 移动开发技术论坛

J2ME List_Multiple 多选模式

浏览 2501 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2013-04-12  
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.Image;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

//列表多选模式测试  MULTIPLE  案例:
public class List_Multiple_test extends MIDlet implements CommandListener{

	//默认属性  定义
	private final static Command CMD_EXIT = new Command("exit",Command.EXIT,1);
	private final static Command CMD_BACK = new Command("BACK",Command.BACK,1);
	private Display display;
	private List mainList;
	private List exclusiveList;
	private List implicitList;
	private List multipleList;
	private boolean firstTime;

	//编写构造方法
	public List_Multiple_test(){
		//获取对象
		display = Display.getDisplay(this);

		//初始化数组 ,Choice 文字选项部分
		String[] stringArray = {
				"Option A",
				"Option B",
				"Option C",
				"Option D",
		};

		//Image[] 数组初始化
		Image[] imageArray = null;

		//exclusiveList 声明
		exclusiveList = new List("ExclusiveList",Choice.EXCLUSIVE,stringArray,imageArray);
		exclusiveList.addCommand(CMD_BACK);
		exclusiveList.addCommand(CMD_EXIT);
		exclusiveList.setCommandListener(this);

		//implicitList 的声明,隐含模式
		implicitList = new List("ImplicitList",Choice.IMPLICIT,stringArray,imageArray);
		implicitList.addCommand(CMD_BACK);
		implicitList.addCommand(CMD_EXIT);
		implicitList.setCommandListener(this);

		//multipleList 声明   多选模式
		multipleList = new List("Multiple",Choice.MULTIPLE,stringArray,imageArray);
		multipleList.addCommand(CMD_BACK);
		multipleList.addCommand(CMD_EXIT);
		multipleList.setCommandListener(this);

		firstTime = true;
	}
	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
		if(firstTime){
			Image[] imageArray = null;
			try{
				Image icon = Image.createImage("/images/eclipse.png");// 路径为相对路径
				imageArray = new Image[]{icon,icon,icon};
			}catch(Exception ex){
				ex.printStackTrace();
			}

			String[] stringArray = {"Exclusive","Implicit","Multiple"};
			mainList = new List("Choose Type",Choice.IMPLICIT,stringArray,imageArray);
			mainList.addCommand(CMD_BACK);
			mainList.addCommand(CMD_EXIT);
			mainList.setCommandListener(this);
			display.setCurrent(mainList);
			firstTime = false;
		}
	}

	public void commandAction(Command c, Displayable d) {
		// TODO Auto-generated method stub
		if(d.equals(mainList)){
			//使用了隐含模式 , 选择的时候会以SELECT_COMMAND 作为第一参数传入
			if(c==List.SELECT_COMMAND){
				switch (((List)d).getSelectedIndex()) {
				case 0:
					display.setCurrent(exclusiveList);
					break;
				case 1:
					display.setCurrent(implicitList);
					break;
				case 2:
					display.setCurrent(multipleList);
					break;
				}
			}else{
				if(c==CMD_BACK){
					display.setCurrent(mainList);
				}
			}
			if(c==CMD_EXIT){
				try {
					destroyApp(false);
				} catch (MIDletStateChangeException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				notifyDestroyed();
			}
		}
	}

}
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics