MIDP中的事件处理机制在J2ME用户图形界面开发中具有举足轻重的地位,本文将深入分析MIDP高级事件处理机制,本文提供的程序是新手学习不错的例子。在接下来的文章我会继续讲述低级事件处理机制。
MIDP的事件处理处理机制同样是采用回调的机制,你可以参考AWT的事件处理原理。在MIDP中分高级事件处理和低级事件处理,前者主要是针对高级UI,而后者主要是这对Canvas这样的低级UI的。不管怎样,我们必须首先介绍的是Command类,移动信息设备的输入主要来自于手机的键盘,导航则主要是由按钮来完成的,例如软键和导航键。Command类包装了动作的信息,但是并没有包括动作触发后应该做什么,这些是在CommanListener内实现的。看看Command的构造器吧
Command command = new Command("退出",Command.EXIT,1),构造器的参数分别表示按钮的标签,按钮的类型和优先级别。移动信息设备是根据后面两个参数对按钮进行布局的。
高级事件处理主要由两个接口来实现,一个是CommandListener,一个是ItemStateListener。在CommandListener中定义了方法commandAction(Command cmd,Displayable disp),我们必须实现这个方法来完成事件处理,告诉应用程序当在disp界面下如果cmd按钮并按下的时候他应该去做什么。ItemStateListener中定义了方法itemStateChanged(Item item),应用程序通过他来得到在Form内的item内部状态发生变化的事件。用户在进行如下操作的时候都会触发这样的事件,调整交互性Guage、在TextField内输入或者修改内容、修改DateField、改变ChoiceGroup的状态。
尽管我们已经通过上面的介绍了解了事件处理的机制,但是你会发现我们进行用户界面导航的时候仍然非常的麻烦,因为这不是在浏览器上开发界面,我们能做的只能是通过Command来进行处理。下面我将通过一个例子来说明如何使用CommandListener和ItemStateListener,这个例子有两个界面一个界面用来输入另一个用来显示用户的输入。在输入界面如果我们选择click这个ChoiceGroup,界面会出现另外一个文本输入,如果我们不选择他就不会出现。在这样一个程序中充分说明了如何使用上述两个接口来完成MIDP高级事件的处理,但是也暴露出了导航难得问题,在以后的文章中我会介绍如何使用MVC的设计模式来解决这个问题。下面是程序执行的界面抓图
程序源代码如下所示:
//HighLevelMIDlet.java
import javax.microedition.lcdui.Display;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
/*
* Created on 2004-6-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class HighLevelMIDlet extends MIDlet
{
private Display display;
private MainUI mainUI;
private DisplayUI displayUI;
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#startApp()
*/
protected void startApp() throws MIDletStateChangeException
{
// TODO Auto-generated method stub
initMIDlet();
}
private void initMIDlet()
{
display = Display.getDisplay(this);
mainUI = new MainUI(this);
displayUI = new DisplayUI(this,mainUI);
display.setCurrent(mainUI);
}
public DisplayUI getDisplayUI()
{
return displayUI;
}
public Display getDisplay()
{
return display;
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#pauseApp()
*/
protected void pauseApp()
{
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see javax.microedition.midlet.MIDlet#destroyApp(boolean)
*/
protected void destroyApp(boolean arg0) throws MIDletStateChangeException
{
// TODO Auto-generated method stub
}
}
//MainUI.java
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
/*
* Created on 2004-6-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class MainUI extends Form implements CommandListener, ItemStateListener
{
private HighLevelMIDlet midlet;
private TextField inputField;
private ChoiceGroup choice;
private TextField inputField2;
private int index;
public static final Command cmd = new Command("Display", Command.ITEM, 2);
public MainUI(HighLevelMIDlet midlet)
{
super("form");
this.midlet = midlet;
init();
}
public void init()
{
inputField = new TextField("Input", null, 25, TextField.ANY);
inputField2 = new TextField("Input2", null, 25, TextField.ANY);
choice = new ChoiceGroup("click", Choice.MULTIPLE);
choice.append("Another", null);
this.append(inputField);
this.append(choice);
this.addCommand(cmd);
this.setCommandListener(this);
this.setItemStateListener(this);
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1)
{
// TODO Auto-generated method stub
if(arg0 == cmd)
{
String input = inputField.getString();
System.out.println(input);
midlet.getDisplayUI().setInput(input);
midlet.getDisplay().setCurrent(midlet.getDisplayUI());
}
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.ItemStateListener#itemStateChanged(javax.microedition.lcdui.Item)
*/
public void itemStateChanged(Item arg0)
{
// TODO Auto-generated method stub
if(arg0 == choice)
{
if(choice.isSelected(0) == true)
{
index = this.append(inputField2);
}
else
{
this.delete(index);
}
}
}
}
//DisplayUI.java
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
/*
* Created on 2004-6-24
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
/**
* @author P2800
*
* TODO To change the template for this generated type comment go to Window -
* Preferences - Java - Code Style - Code Templates
*/
public class DisplayUI extends Form implements CommandListener
{
private HighLevelMIDlet midlet;
private StringItem displayItem;
private Displayable backUI;
public static final Command backCommand = new Command("Back", Command.BACK,
2);
public DisplayUI(HighLevelMIDlet midlet,Displayable backUI)
{
super("Display");
this.midlet = midlet;
this.backUI = backUI;
init();
this.addCommand(backCommand);
this.setCommandListener(this);
}
private void init()
{
displayItem = new StringItem("you input", null);
this.append(displayItem);
}
public void setInput(String input)
{
displayItem.setText(input);
}
/*
* (non-Javadoc)
*
* @see javax.microedition.lcdui.CommandListener#commandAction(javax.microedition.lcdui.Command,
* javax.microedition.lcdui.Displayable)
*/
public void commandAction(Command arg0, Displayable arg1)
{
// TODO Auto-generated method stub
if(arg0 == backCommand)
{
midlet.getDisplay().setCurrent(backUI);
}
}
}
本文地址:http://com.8s8s.com/it/it16309.htm