In-depth analysis of the MIDP Advanced event processing mechanism on the j2_based Platform

Source: Internet
Author: User

The event processing mechanism in MIDP plays an important role in the development of the graphic interface for the users of the users. This article will analyze the MIDP Advanced event processing mechanism in depth. The program provided in this article is a good example for beginners to learn. In the following article, I will continue to describe the low-level event processing mechanism.

The MIDP event processing mechanism also uses the callback mechanism. You can refer to the AWT event processing principle. In MIDP, advanced event processing and low-level event processing are performed. The former is mainly for the advanced UI, while the latter is mainly for the low-level UI such as canvas. In any case, we must first introduce the command class. The input of mobile information devices mainly comes from the keyboard of the mobile phone, and the navigation is mainly completed by buttons, such as soft keys and navigation keys. The command class encapsulates the action information, but does not include what should be done after the action is triggered. These are implemented in commanlistener. Let's look at the command constructor.
Command command = new command ("exit", command. Exit, 1). Parameters of the constructor indicate the button labels, button types, and priority levels respectively. Mobile information devices are arranged based on the following two parameters.

Advanced event processing is implemented by two interfaces: commandlistener and itemstatelistener. The commandaction (command cmd, displayable disp) method is defined in commandlistener. We must implement this method to complete event processing, tell the application what it should do when the CMD button is pressed on the disp interface. The itemstatelistener defines the itemstatechanged (item) method. The application uses this method to obtain the event in which the internal state of the item in the form changes. When you perform the following operations, you will trigger such an event to adjust interactive guage, input or modify content in textfield, modify datefield, and change the choicegroup status.

Although we have learned about the event processing mechanism through the above introduction, you will find that we are still very troublesome in user interface navigation, because this is not a browser development interface, what we can do is to use command for processing. The following is an example of how to use commandlistener and itemstatelistener. In this example, there are two interfaces on which one interface is used to input the other interface to display user input. On the input page, if we click this choicegroup option, another text input will appear. If we do not select this option, it will not appear. In such a program, we fully explain how to use the above two interfaces to process the MIDP advanced events, but it also exposes a rare problem in navigation, in future articles, I will introduce how to use the MVC design pattern to solve this problem. The following figure shows the execution interface of the program.


The program source code is as follows:

// 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
* Window-preferences-Java-code style-code templates
*/

/**
* @ Author p2800
*
* Todo to change the template for this generated type comment go
* Window-preferences-Java-code style-code templates
*/
Public class highlevelmidlet extends MIDlet
{

Private display;
Private mainui;
Private 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
* 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
* 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 );
}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.