高级UI提供了一个Alert的控件可以有弹出对话框的效果。但是大家想不想自己实现一个呢。想不想知道sun是如何让Alert工作的呢?好请看下文
设计思想是。建立一个 abstract class Dialog extends Canvas。下面的事情就让我们一步步花出来吧。
实现理念是先绘制整个Canvas然后通过图形Graphics描绘成透明,接着在整个屏幕的中间去出一块区域来,至于上面你要做什么那就是你的事情了。哈。
好看代码,
java 代码
-
-
-
-
-
- package org.pook.ui.form;
-
- import java.util.TimerTask;
-
- import javax.microedition.lcdui.Canvas;
- import javax.microedition.lcdui.Display;
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
-
- import org.pook.log.Log;
- import org.pook.ui.Command;
- import org.pook.ui.SoftButton;
- import org.pook.ui.core.Platform;
- import org.pook.ui.event.CommandListener;
- import org.pook.ui.timer.TimerTaskManager;
-
-
-
-
-
-
-
-
-
-
-
-
- public abstract class Dialog extends Canvas implements Runnable {
- private static Log log = Log.getLog("Dialog");
-
- protected final int X = 0;
-
- protected final int Y = 1;
-
- protected final int WIDTH = 2;
-
- protected final int HEIGHT = 3;
-
-
- protected int[] view = new int[4];
-
-
- public static final int DIALOG_OK = 0;
-
-
- public static final int DIALOG_CANCEL = 2;
-
-
- public static final int DIALOG_YES_NO = 3;
-
-
- public static final int DIALOG_OK_CANCEL = 4;
-
-
- public static final int DIALOG_YES_NO_CANCEL = 5;
-
- public static final int DIALOG_TEXT = 6;
-
-
-
-
- protected Panel parent;
-
- protected boolean hasFocus;
-
-
-
-
- long timeOut;
-
-
-
-
- int type;
-
-
- protected String title;
-
-
-
-
- protected String[] rowTitle;
-
- protected int bgColor;
-
- protected int fontColor;
-
- protected Font font = Font.getDefaultFont();
-
-
- protected TimerTask task;
-
- protected Display display;
-
- protected SoftButton softButton;
-
-
-
-
-
-
-
-
-
-
-
- public Dialog(String title, Panel parent, Display display) {
- this(title, parent, display, 3000);
- }
-
-
-
-
-
-
-
-
- public Dialog(String title, Panel parent, Display display, long timeOut) {
- this(title, parent, display, Dialog.DIALOG_OK, timeOut);
- }
-
-
-
-
-
-
-
-
-
- public Dialog(String title, Panel parent, Display display, int type,
- long timeOut) {
- setFullScreenMode(true);
-
- this.parent = parent;
- checkParent();
-
- this.timeOut = timeOut;
- this.type = type;
- this.title = title;
- this.display = display;
-
- softButton = new SoftButton();
-
-
-
- if (timeOut != 0)
- task = TimerTaskManager.getInstace().add(this, timeOut);
-
-
- setStyle(0x0033FF, 0xFFFFFF);
-
- }
-
- public void setStyle(int bgColor, int fontColor) {
- this.bgColor = bgColor;
- this.fontColor = fontColor;
- }
-
- public void cancel() {
-
- if (parent == null)
- throw new NullPointerException("Parent is not Null.");
-
- task.cancel();
- if(parent == null)
- return;
-
- display.setCurrent(parent);
-
- }
-
- public void addCommand(Command cmd) {
- this.softButton.addCommand(cmd);
- }
-
- public void setSoftButtonListener(CommandListener cml) {
- this.softButton.setCommandListener(cml);
- }
-
- public void setSoftButtonStyle(int bgColor, int fontColor){
- this.softButton.setStyle(bgColor, fontColor);
- }
-
- public void run() {
- cancel();
-
- }
-
-
- public void drawRGB(Graphics g) {
- int ai[] = new int[Platform.WIDTH];
- for (int j1 = 0; j1 < ai.length; j1++)
- ai[j1] = 0x90000000;
- g.drawRGB(ai, 0, 0, 0, 0, Platform.WIDTH, Platform.HEIGHT, true);
- }
-
-
-
-
-
- private void checkParent() {
- if (parent == null){
- throw new NullPointerException("Parent is not null");
- }
-
- }
-
- protected void keyPressed(int keyCode) {
- softButton.onClick(keyCode);
-
- }
-
- public void setPanel(Panel panel) {
- this.parent = panel;
-
- }
-
- public void setTimeOut(long timeOut) {
- this.timeOut = timeOut;
- }
-
- public int getType() {
- return type;
- }
-
- public void setType(int type) {
- this.type = type;
- }
- }
下面这段是弹出一个对话框实现
java 代码
-
-
-
-
-
- package org.pook.ui.form;
-
- import java.util.Vector;
-
- import javax.microedition.lcdui.Font;
- import javax.microedition.lcdui.Graphics;
-
- import org.pook.Pook;
- import org.pook.log.Log;
- import org.pook.ui.Command;
- import org.pook.ui.SelectedPart;
- import org.pook.ui.core.Platform;
- import org.pook.ui.event.CommandListener;
- import org.pook.ui.util.FontUtil;
- import org.pook.ui.util.GraphicsUtil;
- import org.pook.util.StringUtil;
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public class MessageDialog extends TextDialog implements CommandListener{
- private static Log log = Log.getLog("MessageDialog");
-
-
- private Command ok;
-
- private SelectedPart plan;
- private int planHeight;
-
- private int contentHeight;
-
- private int viewSize;
- private int space;
-
-
- private Vector content;
- private int selectIndex;
- private int contentY;
-
- public MessageDialog(String _title,String _message, long _timeOut) {
- super(_title, Pook.MAINMENU, Pook.getDisplay(), _timeOut);
- ok = new Command("确定",Command.LEFT, Command.FIRST_PRIORITY);
- this.addCommand(ok);
- this.setSoftButtonListener(this);
- this.setMessage(_message);
- initView();
- this.setStyle(0x001D68, 0xFFFFFF);
- this.setSoftButtonStyle(0x0071BD,0xFFFFFF);
- this.setType(Dialog.DIALOG_OK);
- }
-
- public void setMessage(String message){
-
- skipMessage(message);
- }
-
-
- private void skipMessage(String message) {
-
- if(message == null)
- return ;
-
- content = new Vector();
- String[] text = StringUtil.split(message, "\n");
-
- for(int j =0; j < text.length; j++){
-
- String[] t = FontUtil.splitOnlyStringToRowString(font,text[j],view[WIDTH]-6);
- for(int i=0; i
- content.addElement(t[i]);
- }
-
- }
-
-
-
-
-
-
-
-
-
- }
-
- protected void keyPressed(int keyCode) {
- super.keyPressed(keyCode);
- this.thisUpAndDown(keyCode);
- this.repaint();
- }
-
-
-
-
-
-
- private void thisUpAndDown(int keyCode) {
-
- if (this.content == null)
- return;
-
- if(this.content.size() < this.viewSize)
- return;
-
- switch (keyCode) {
-
- case Platform.KEY_UP: {
-
- selectIndex = selectIndex <= 0 ? content.size() - viewSize - 1
- : --selectIndex;
-
- break;
-
- }
- case Platform.KEY_DOWN: {
- selectIndex = selectIndex >=content.size() - viewSize - 1 ? 0
- : ++selectIndex;
-
-
- break;
- }
- }
- }
-
-
- public void commandAction(Command cmd) {
- if(cmd == ok)
- this.cancel();
-
- }
- protected void paint(Graphics g) {
- parent.paint(g);
- drawRGB(g);
- this.softButton.paint(g);
- this.paintMessageDialog(g);
-
- }
-
-
-
- private void paintMessageDialog(Graphics g) {
- initView();
- g.setFont(font);
- GraphicsUtil.drawRectWithColor(g,0xFFD84F, view[X]-1,view[Y]-1,view[WIDTH]+1,view[HEIGHT]+1);
- paintTitleImpl(g);
- paintContent(g);
- paintState(g);
- }
-
- private void paintTitleImpl(Graphics g) {
- g.setColor(0x0071BC);
- g.fillRect(view[X],view[Y],view[WIDTH],font.getHeight() + 2);
- g.setColor(0xFFFFFF);
- g.drawString(title, view[X] + 4, view[Y] + 1,Graphics.TOP|Graphics.LEFT);
- }
-
- private void paintContent(Graphics g) {
- if(content.size() > viewSize ){
- this.plan = new SelectedPart();
- plan.setStyle(0xFFFFFF, 0x000000);
- this.planHeight = contentHeight/(content.size() - viewSize);
- }
- contentY = view[Y] + font.getHeight() + 4;
-
- g.setColor(0x001D68);
- g.fillRect(view[X],contentY,view[WIDTH],contentHeight);
-
- paintContentImpl(g);
-
- }
-
- private void paintContentImpl(Graphics g) {
- if(content == null)
- return ;
-
- int size = viewSize>content.size()?content.size():viewSize + selectIndex;
-
- g.setColor(0xFFFFFF);
-
- for(int i = selectIndex; i
- g.drawString((String) content.elementAt(i),view[X] + 2, contentY + space * (i-selectIndex), Graphics.TOP|Graphics.LEFT);
- }
-
-
-
-
- if(plan != null){
-
- plan.changePosition(view[WIDTH] + 4 , contentY + this.planHeight *selectIndex, 4,this.planHeight);
- plan.paint(g);
- }
- }
-
- private void paintState(Graphics g) {
- int height = Platform.HEIGHT - 30 - font.getHeight() - 2;
- g.setColor(0x0071BC);
- g.fillRect(view[X], height ,view[WIDTH],font.getHeight() + 2);
- g.setColor(0xFFFFFF);
- g.drawLine(view[X],height,view[WIDTH] + 10, height);
- g.setColor(0xFFFFFF)
|