Dialog components are not provided in the MIDP, but an Alert is provided. Alert is limited, so writing a Dialog component is very necessary. This article will provide a MIDP-based Dialog component that you can use in your application, powerful and very convenient.
When we develop applications, sometimes you need to ask users to continue the following operations, such as deleting a phone number, and then enter different processes based on different action. At this time, we need a decent Dialog component, unfortunately there is no supply in the MIDP, but we can write one by Canvas. The design of this component will be briefly introduced, and then give the source code for the MIDlet of the test. I hope to help readers!
First we write an abstract class Dialog, the content is as follows / * * Created on 2004-7-10 * * 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 * /
Import javax.microedition.lcdui. *;
// The base class for a set of general-purpose // Dialogs. To use, create a concrete instance // of this class, set the dialog listener, // and the call display.
Public Abstract Class Dialog {Protected DISPLAY; Protected DISPLISTENER LISTENER; Protected Displayable Restore; Private Int EventId;
Protected DILOG (Display Display) {this.display = display;
/ ** * @Return Returns the EventId. * / Public int getEventId () {Return EventId;
/ ** * @Param Eventid * the evenetid to set. * / Public void setEventId (int eventid) {this.Eventid = EventId;}
// dismisses the dialog, restoring the old // Displayable, if any. This is normally done // by The Dialog Itself in response to commands, // but can also be called by the application // in response to some Other Event, Such as a // timer expression. The code is passed directly // to the dialog listener, if any.
public void dismiss (int code) {Displayable curr = display.getCurrent (); if (! curr = getDisplayable ()) return; if (restore = null!) {display.setCurrent (restore);} else {display.setCurrent ( NEW form ("" "));
IF (listener! = null) {listener.dialogdismissed (this, code);}}
// Displays the dialog, saving the current // Displayable for Later Restoration.
Public void display () {Displayable curr = display.getcurrent (); displayable dialog = getDisplayable ();
IF (Curr! = Dialog) {restore = curr; display.setcurrent (dialog);}}
Public void display (int event) {Displayable curr = display.getcurrent (); displayable dialog = getDisplayable (); this.EventId = event;
IF (Curr! = Dialog) {restore = curr; display.setcurrent (dialog);}}
// Returns The Registered Dialog Listener.
Public Dialoglistener getDialoglistener () {returnis
// Subclasses Implement this method to return // the actual object to display on the screen.
protected abstract displayable getDisplayable ();
// registers a dialog listener.
Public void setDialoglistener (Dialoglistener L) {listener = L;
}
You need to override the getDisplayable () method to return a Displayable object, when you call Dialog's Display () method, your YourDialog will appear on the screen, sometimes you might want to pass an event value to the object, Then you should call Method Display (int evenet). Dialog can register Dialoglistener, this interface defines a method, the content is as follows: / * * Created on 2004-7-10 * * 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 interface DialogListener {void dialogDismissed (Dialog dialog, int code) When Dialog is displayed, we provide the user's interface is implemented with Waitcanvas. Here is his code: / * * Created on 2004-3-14 * * to change the template for this generated file go to * window - Preferences - Java - Code Generation - Code and Comments * /
Import java.util. *; import javax.microedition.lcdui. *;
/ ** * @Author p2800 * * to change the template for this generated type comment go to window - * preferences - java - code generation - code and commerss * / public class waitcanvas extends canvas {
Private int mcount, mmaximum; private int minterval;
Private Int Mwidth, MRADIHT, MX, MX, MRADIUS; Private String Mimentage; Private Boolean Run = FALSE
Public Waitcanvas (String Message, Boolean Run {this.mmessage = message; mcount = 0; mmaximum = 36; minterval = 100;
MWIDTH = getWidth (); mahight = getHeight ();
// Calculate the Radius. Int halfwidth = (MWIDTH - MRADIUS) / 2; int halfheight = (MRATION) / 2; MRADIUS = Math.min (HalfWidth, Halfheight);
// Calculate the location. Mx = HALFWIDTH - MRADIUS / 2; My = HALFHEIGHT - MRADIUS / 2; // Create a Timer to update the display. If (run) {TIMERTASK TASK = New Timertask () {public void Run () {Mcount = (mcount 1)% mmaximum; repaint ();}}; timer timer = new time ();
Timer.schedule (task, 0, minterval);}}
Public void Paint (Graphics G) {INT THETA = - (McOUNT * 360 / MMaximum);
// Clear The Whole Screen. G.SetColor (255, 255, 255); g.fillRect (0, 0, MWIDTH, MWIGHT);
// Now Draw The Pinwheel. G.SetColor (128, 128, 255); G.drawarc (MX, MX, MRADIUS, MRADIUS, 0, 360); G. Fillarc (MX, MX, MX, MRADIUS, MRADIUS, THETA 90 , 90); G. Fillarc (MX, MX, MRADIUS, MRADIUS, THETA 270, 90);
// Draw the message, if there is a message. If (mmessage! = Null) {g.drawstring (MMessage, Mwidth / 2, MHEIGHT, Graphics.bottom | graphics.hcenter);}}
}
By controlling the value of Boolean Run can determine whether this picture is moved. Below two examples are Dialog's subclasses, they have achieved its abstract method. I give code directly: import javax.microedition.lcdui. *;
// defines a dialog That Displays a text // message and "yes" and "no" buttons.
Public Class ConfirmationDialog Extends Dialog IMPLEments CommandListener {
public static final int YES = 0; public static final int NO = 1; protected Canvas canvas; protected Command noCommand; protected Command yesCommand; private String message; private String yesLabel; private String noLabel;
public ConfirmationDialog (Display display, String message) {this (display, message, null, null);} public ConfirmationDialog (Display display, String amessage, String ayesLabel, String anoLabel) {super (display); this.message = (amessage = = NULL)? "Continue?": AMessage; this.yeslabel = (YESLABEL == NULL)? "OK": ayeslabel; this.nolabel = (Nolabel == NULL)? "Return": Anolabel;
Yescommand = New Command (Yeslabel, Command.ok, 1); Nocommand = New Command (Nolabel, Command.cancel, 1);
Canvas = New Waitcanvas (Message, True); YesCommand; Canvas.Addcommand (NoCommand); Canvas.SetCommandListener (this);}
/ ** * @Return Returns the message. * / Public string getMessage () {Return Message;
/ ** * @Param message * the message to set. * / Public void setment {this.Message = mess;}
Public Void CommandAction (Command C, Displayable D) {if (c == yescommand) {Dismiss (YES);} else if (c == nocommand) {Dismiss (no);}}
Protected Displayable getDisplayable () {Return Canvas;}
}
Import javax.microedition.lcdui. *;
Public Class MessageDialog Extends Dialog IMPLEments CommandListener {
PUBLIC Static Final Int OK = 0; Protected Command Command; Private String Message; Private String Label;
Public MessageDialog (Display Display, String Message) {this (Display, Message, null);}
Public MessageDialog (Display Display, String Amessage, String Alabel) {Super (Display); this.Message = (AMESSAGE == NULL)? "Complete": AMessage; this.Label = (Alabel == NULL)? "OK": Alabel; Command = New Command (Label, Command.ok, 1); Canvas = New Waitcanvas (Message, True); canvas.addcommand (Command); canvas.setcommandlistener (this);}
Public void CommandAction (Command C, Displayable D) {if (c == command) {DISMISS (OK);}}
Protected Displayable getDisplayable () {Return Canvas;}
}
You can easily use these two Dialog in your own program, you can also extend the Dialog class to implement your own Dialog. Here is the MIDlet that tests these two Dialog. First let's take a look at its screenshot and give the source code! Only the user selects the determined interface here is given, if you choose to return, then the literal change below is given. Import javax.microedition.lcdui. *; import javax.microedition.midlet. *;
Public Class Dialogtest Extends Midlet ImmmandListener, Dialoglistener {
Private Display Display; private confirmationDialog confirm; private messagedialog message;
Public Static Final Command EXITCOMMAND = New Command ("EXIT", Command.exit, 1);
Public dialogtest () {}
Public void CommandAction (Command C, Displayable D) {if (c == exitcommand) {exitmidlet ();}}
Protected Void DestroyApp (Boolean Unconditional) THROWS MIDLETSTATECHANGEXCEPTION {EXITMIDLET ();
Public void exitmidlet () {notifydestroyed ();
Public Display getDisplay () {Return Display;
protected void initmidlet () {}
protected void pauseapp () {}
Protected Void Startapp () throws midletStateChangeException {if (display == null) {display = display.getdisplay (this); initmidlet ();} //first ask the user a question
Confirm = New ConfirmationDialog (Display, "Continue?"); confirm.SetDialoglistener (this); confirm.display ();
Public Void Dialogdismissed (Dialog D, INT Code) {IF (D == Confirm) {// Then Give The User A Response
IF (code == confirmationDialog.yes) {message = new messageDialog (DISPLAY, "You Choose OK");} else {message = new messageDialog (DISPLAY, "You have selected it");} message.display (); Message.SetDialoglistener (this);} else if (d == message) {// now let the user quit
Form f = new form (null); f.Append ("exit program"); f.addcommand; f.setcommandlistener (this); display.setcurrent (f);}}
}
The entire code above is a complete Dialog component (excluding dialogtest) if you need to use it directly in your project!