When writing a mobile phone program, you often need to switch between screens. I didn't know how to do it at first. I read an example in the past two days. It's very simple, but it can explain the problem. To sum up. If anything is wrong, please correct it!
Function: there are multiple buttons in the main program. Each button corresponds to a function, and each function must be displayed on a different screen (element.
Implementation:
1. The main program must define a Display object, such as private Display display, which indicates the current screen. There are some Displayable objects. For example, Form and TextField are all subclasses of Displayable. In the main program, you can add a Displayable instance to the current screen by using dipslay. serCurrent (Displayable Instance name. The following procedure:
Private Display display;
Private Form form = new Form ("one Form");
Public void startApp ()
{
Display. setCurrent (form );
}
The role is to add the form to the current screen.
2. to switch between screens, you just need to put the display stuff you want to display into the display object of the main program. If a display is defined in the main program, the display of the main program will be referenced in another screen (I'll call it a target screen.
Development Environment: Win2000 server + Eclipse + WTK2.1
Use the following code:
MainMIDlet. java: the main program, a standard MIDlet.
Import javax. microedition. midlet. MIDlet;
Import javax. microedition. lcdui .*;
Public class MainMIDlet extends MIDlet implements CommandListener {
Private Display display;
Private Form form = new Form ("Wellcome !! ");
Private Command okCommand = new Command ("OK", Command. OK, 1); // Select OK to switch to the next Screen
Private Form ns;
Private StringItem si = new StringItem ("First Screen ","~ _~ ");
Public MainMIDlet ()
{
Form. addCommand (okCommand );
Form. append (si );
}
Public void startApp (){
Display = Display. getDisplay (this );
Display. setCurrent (form );
Form. setCommandListener (this); // Add CommandListener to form
}
Public void pauseApp (){
}
Public void destroyApp (boolean B ){
}
Public void commandAction (Command c, Displayable s)
{
If (c = okCommand)
{
Ns = new NextScreen (display, form); // the most important part is here :)