基于MIDP实现Dialog组件
来源:互联网 发布:网络与信息安全专业 编辑:程序博客网 时间:2024/06/07 07:05
在MIDP中没有提供Dialog组件,但是提供了一个Alert。Alert的功能有限,因此写一个Dialog组件是非常有必要的。本文将提供一个基于MIDP的Dialog组件,你可以在应用程序中使用它,功能强大且非常方便。
当我们开发应用程序的时候,有的时候需要询问用户是不是要继续下面的操作,比如删除一个电话号码,然后根据用户的不同的动作进入不同的流程。这时候我们需要一个像样的Dialog组件,很遗憾MIDP中并没有提供,但是我们可以用Canvas自己写一个。下面将简单介绍这个组件的设计,然后给出测试的MIDlet的源代码。希望对读者有帮助!
首先我们写一个抽象类Dialog,内容如下/* * 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 then call display.
public abstract class Dialog{ protected Display display; protected DialogListener listener; protected Displayable restore; private int eventID;
protected Dialog(Display display) { this.display = display; }
/** * @return Returns the eventID. */ public int getEventID() { return eventID; }
/** * @param eventID * The eventID 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 expiration. 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() { return listener; }
// 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; }
}
你需要覆盖getDisplayable()方法返回一个Displayable的对象,当你调用Dialog的display()方法的时候,你的YourDialog将会显示在屏幕上,有的时候你可能要传递一个事件值给后面的对象,那么你应该调用方法display(int event)。Dialog可以注册DialogListener,这个接口定义了一个方法,内容如下:/* * 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);}当Dialog显示的时候,我们提供给用户的界面是用WaitCanvas实现的,下面是他的代码:/* * 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 Comments */public class WaitCanvas extends Canvas{
private int mCount, mMaximum; private int mInterval;
private int mWidth, mHeight, mX, mY, mRadius; private String mMessage; private boolean run = false;
public WaitCanvas(String message, boolean run) { this.mMessage = message; mCount = 0; mMaximum = 36; mInterval = 100;
mWidth = getWidth(); mHeight = getHeight();
// Calculate the radius. int halfWidth = (mWidth - mRadius) / 2; int halfHeight = (mHeight - mRadius) / 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 Timer();
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, mHeight);
// Now draw the pinwheel. g.setColor(128, 128, 255); g.drawArc(mX, mY, mRadius, mRadius, 0, 360); g.fillArc(mX, mY, mRadius, mRadius, theta + 90, 90); g.fillArc(mX, mY, 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); } }
}
通过控制boolean run的值可以决定是不是让这个画面动起来。下面两个例子是Dialog的子类,他们实现了它的抽象方法。我直接给出代码: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) ? "继续操作?" : amessage; this.yesLabel = (yesLabel == null) ? "确定" : ayesLabel; this.noLabel = (noLabel == null) ? "返回" : anoLabel;
yesCommand = new Command(yesLabel, Command.OK, 1); noCommand = new Command(noLabel, Command.CANCEL, 1);
canvas = new WaitCanvas(message, true); canvas.addCommand(yesCommand); canvas.addCommand(noCommand); canvas.setCommandListener(this); }
/** * @return Returns the message. */ public String getMessage() { return message; }
/** * @param message * The message to set. */ public void setMessage(String message) { this.message = message; }
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; protected Canvas canvas; 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)?"完成":amessage; this.label = (alabel == null)?"确定":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; }
}
你可以方便的在自己的程序中使用这两个Dialog,你也可以扩展Dialog类实现自己的Dialog,下面是测试这两个Dialog的MIDlet。首先我们看一下它的截图然后给出源代码!这里只给出用户选择确定的界面,如果你选择返回那么知识下面的文字改变。import javax.microedition.lcdui.*;import javax.microedition.midlet.*;
public class DialogTest extends MIDlet implements CommandListener, DialogListener{
private Display display; private Form mainForm; 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 MIDletStateChangeException { 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, "继续操作嘛?"); 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, "您选择了确定"); } else { message = new MessageDialog(display, "您选择了返回"); } message.display(); message.setDialogListener(this); } else if (d == message) { // Now let the user quit
Form f = new Form(null); f.append("退出程序"); f.addCommand(exitCommand); f.setCommandListener(this); display.setCurrent(f); } }
}
上面的整个代码是一个完整的Dialog组件(不包括DialogTest)如果需要使用直接放在你的project里面就可以了!
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>- 基于MIDP实现Dialog组件
- 基于MIDP实现Dialog组件
- 基于MIDP实现Dialog组件
- 基于MIDP实现ResourceBundle类
- 自己动手实现基于MIDP的ResourceBundle类
- 自己动手实现基于MIDP的ResourceBundle类
- 使用 MIDP 列表组件
- 基于MIDP实现ResourceBundle类 (转J2meDev.com)
- android基于dialog实现等待加载框
- Android基于Dialog实现加载框
- [Apache Click快速开发]基于Jquery Dialog的Click Dialog组件
- MIDP
- 改写控件之《基于MaterialDialog实现的评分Dialog》
- CLDC及MIDP参考实现的编译
- 贪吃蛇游戏的MIDP实现
- 在J2ME/MIDP中实现图像旋转
- 扩展Dialog组件
- jqueryui的dialog组件
- 发布 Media Player for Eclipse 0.0.1
- 发布 Media Player for Eclipse 0.0.2
- 中国经营报080423
- 1. Foreword
- sql行改為列
- 基于MIDP实现Dialog组件
- 一次遍历找出“出现次数最多的子串”
- 触摸Java中的包和文档
- Java多线程编程初步
- SWT学习笔记(一)-在项目中加入swt.jar
- 简单客户系统的权限控制实现
- 2008-4-25周市场分析
- Java 库的建立方法及其实例
- 设计之美——命令模式(二)