The event handling mechanism in the MIDP has a lightweight position in the J2ME user graphical interface development. This article will analyze the MIDP advanced event handling mechanism. The program provided herein is an example of novice learning. In the next article I will continue to tell the lower event handling mechanism.
MIDP's event handling mechanism is also a mechanism for callback, you can refer to the AWT's event processing principle. In MIDP mid-range advanced event handling and low-level event processing, the former is mainly for advanced UI, while the latter is mainly this low UI such as Canvas. In any case, we must first introduce the Command class. The input of mobile information devices is mainly from the keyboard of the phone. Navigation is mainly done by buttons, such as soft keys, and navigation keys. The Command class wraps the information of the action, but does not include what should be done after the action is triggered, which is implemented in CommanListener. Take a look at the Command's constructor. Command command = new command ("Exit", command.exit, 1), the parameters of the constructor indicate the tag of the button, the type and priority of the button. The mobile information device is layout according to the two two parameters.
Advanced event processing is mainly implemented by two interfaces, one is CommandListener, one is ItemStateListener. In CommandListener, you define methods CommandAction (Command CMD, Displayable DISP), we must implement this method to complete event processing, tell the application what should he do when the CMD button is pressed in the DISP interface. Method ItemStateChanged (Item Item) is defined in ItemStateListener, and an application has changed events in the ITEM internal status within the Form. The user triggers such an event when performing the following operations, adjusting the interactive Guage, inputs or modifies the content in TextField, modifying DateField, and changing the status of ChoiceGroup.
Although we have learned the mechanism of event processing by introducing the above introduction, you will find that we are still very troublesome when we have user interface navigation, because this is not in the browser to develop interfaces, we can only use Command. Treatment. Below I will explain how to use CommandListener and ItemStateListener through an example, this example has two interfaces to enter another input to display the user. In the input interface If we select Click this choicegroup, another text is displayed in the interface. If we don't choose to choose him, it will not appear. In such a program, you will fully explain how to use the above two interfaces to complete the processing of the MIDP advanced event, but also expose the navigation rare problem, in the later article, I will introduce how to use the MVC design mode to solve this problem. Below is the interface capture of the program execution
The program source code is as follows:
//HighLevelMIDlet.javaimport 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.javaimport 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 THE Template for this generated type comment go to window - * preferences - java - code style - code templates * / public class mainui extends form imports commandlistener, itemStatelistener {@ @ cide templates * / public clarshiplistener {
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, TEXTFIELD2 = New TextField ("Input2", NULL, 25, TEXTFIELD.AnY); Choice = New ChoiceGroup ("click" Choice.multiple; kice.append ("another", null); 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 infut = 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.javaimport 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; 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);
}
}