日期:2014-05-20  浏览次数:20768 次

J2ME学习笔记_3

今天学习了给按钮增加事情和LIST

?

增加事件方法1

package com.hoocy;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MIDlet3 extends MIDlet {

	private Form form = new Form("测试command命令");
	private Command cmd1 = new Command("测试按钮", Command.SCREEN, 1);
	private Display display;

	protected void startApp() throws MIDletStateChangeException {
		display = Display.getDisplay(this);
		display.setCurrent(form);
		form.addCommand(cmd1);
		form.setCommandListener(new Command1());
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
		// TODO Auto-generated method stub
	}

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

class Command1 implements CommandListener {

	public void commandAction(Command c, Displayable d) {
		System.out.println("我被点击了");
		System.out.println(c.getLabel());
	}

}

?增加事件方法2

package com.hoocy;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

public class MIDlet33 extends MIDlet implements CommandListener {

	private Display display;
	private Form form = new Form("title");
	private Command shanchu = new Command("删除", Command.SCREEN, 1);
	private Command genxin = new Command("更新", Command.SCREEN, 1);
	private Command bianji = new Command("编辑", Command.SCREEN, 1);
	private Command back = new Command("返回", Command.BACK, 1);

	public MIDlet33() {
		display = Display.getDisplay(this);
		display.setCurrent(form);
		form.addCommand(shanchu);
		form.addCommand(genxin);
		form.addCommand(bianji);
		form.addCommand(back);
		form.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

	}

	public void commandAction(Command c, Displayable d) {
		if (c == shanchu || c == genxin) {
			form.append(c.getLabel());

		} else if (c == bianji) {
			form.setTitle(c.getLabel());
		} else if (c == back) {
			form.setTitle("BACK");
		}

	}

}

?

关于LIST

package com.hoocy;

import java.io.IOException;

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;

public class MIDlet4 extends MIDlet implements CommandListener{

	private Display display;
	private List list = new List("请选择相应的操作", List.IMPLICIT);

	public MIDlet4() throws IOException {
		display = Display.getDisplay(this);
		display.setCurrent(list);
		list.append("选项一", null);
		list.append("选项二", null);
		list.append("选项三", null);
		Command cmdOk = new Command("确定", Command.OK, 1);
		list.addCommand(cmdOk);
		list.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

	}

	public void commandAction(Command c, Displayable d) {
		int index= list.getSelectedIndex();
		System.out.println(index);
	}
}
?

?

?list? 单选类型2? EXCLUSIVE

?

package com.hoocy;

import java.io.IOException;

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;

public class MIDlet5 extends MIDlet implements CommandListener{

	private Display display;
	private List list = new List("请选择相应的操作", List.EXCLUSIVE);

	public MIDlet5() throws IOException {
		display = Display.getDisplay(this);
		display.setCurrent(list);
		list.append("选项一", null);
		list.append("选项二", null);
		list.append("选项三", null);
		Command cmdOk = new Command("确定", Command.OK, 1);
		list.addCommand(cmdOk);
		list.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

	}

	public void commandAction(Command c, Displayable d) {
		int index= list.getSelectedIndex();
		System.out.println(index);
	}
}
?

?

list---多选

?

package com.hoocy;

import java.io.IOException;

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;

public class MIDlet6 extends MIDlet implements CommandListener{

	private Display display;
	private List list = new List("请选择相应的操作", List.MULTIPLE);

	public MIDlet6() throws IOException {
		display = Display.getDisplay(this);
		display.setCurrent(list);
		list.append("选项一", null);
		list.append("选项二", null);
		list.append("选项三", null);
		Command cmdOk = new Command("确定", Command.OK, 1);
		list.addCommand(cmdOk);
		list.setCommandListener(this);
	}

	protected void destroyApp(boolean arg0) throws MIDletStateChangeException {

	}

	protected void pauseApp() {

	}

	protected void startApp() throws MIDletStateChangeException {

	}

	public void commandAction(Command c, Displayable d) {
		//对于多选取值,有两种方法
		//方法1
//		int size = list.size();
//		for (int i = 0; i < size; i++) {
//			if(list.isSelected(i))
//			{
//				System.out.println(list.getSelectedIndex()+"----"+list.getString(i));
//			}
//		}
		//方法2
		int size = list.size();
		boolean[] flag = new boolean[size];
		list.getSelectedFlags(flag);
		for (int i = 0; i < flag.length; i++) {
			if(flag[i])
			{
				System.out.println(list.getString(i));
			}
		}
	}
}
?