WEB开发网
开发学院软件开发Java 基于MIDP实现Dialog组件 阅读

基于MIDP实现Dialog组件

 2007-12-23 12:32:20 来源:WEB开发网   
核心提示: 在MIDP中没有提供Dialog组件,但是提供了一个Alert,基于MIDP实现Dialog组件,Alert的功能有限,因此写一个Dialog组件是非常有必要的,下面是测试这两个Dialog的MIDlet,首先我们看一下它的截图然后给出源代码!这里只给出用户选择确定的界面,本文将提供一个基于MIDP的Dialog组

   在MIDP中没有提供Dialog组件,但是提供了一个Alert。Alert的功能有限,因此写一个Dialog组件是非常有必要的。本文将提供一个基于MIDP的Dialog组件,你可以在应用程序中使用它,功能强大且非常方便。

当我们开发应用程序的时候,有的时候需要询问用户是不是要继续下面的操作,比如删除一个电话号码,然后根据用户的不同的动作进入不同的流程。这时候我们需要一个像样的Dialog组件,很遗憾MIDP中并没有提供,但是我们可以用Canvas自己写一个。下面将简单介绍这个组件的设计,然后给出测试的MIDlet的源代码。希望对读者有帮助!

 首先我们写一个抽象类Dialog,内容如下

import javax.microedition.lcdui.*;

public abstract class Dialog
{
   PRotected Display display;
   protected DialogListener listener;
   protected Displayable restore;
   private int eventID;

   protected Dialog(Display display)
   {
     this.display = display;
   }

   public int getEventID()
   {
     return eventID;
   }


   public void setEventID(int eventID)
   {
     this.eventID = eventID;
   }

   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);
     }
   }

   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);
     }
   }

   public DialogListener getDialogListener()
   {
     return listener;
   }

   protected abstract Displayable getDisplayable();

   public void setDialogListener(DialogListener l)
   {
     listener = l;
   }

}

   你需要覆盖getDisplayable()方法返回一个Displayable的对象,当你调用dialog的display()方法的时候,你的YourDialog将会显示在屏幕上,有的时候你可能要传递一个事件值给后面的对象,那么你应该调用方法display(int event)。Dialog可以注册DialogListener,这个接口定义了一个方法,内容如下:

public interface DialogListener
{
   void dialogDismissed(Dialog dialog, int code);
}
当Dialog显示的时候,我们提供给用户的界面是用WaitCanvas实现的,下面是他的代码:

import java.util.*;
import javax.microedition.lcdui.*;

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.*;

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 = (amessa


  ge == 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);
   }

   public String getMessage()
   {
     return message;
   }

   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。首先我们看一下它的截图然后给出源代码!
基于MIDP实现Dialog组件(图一)基于MIDP实现Dialog组件(图二)
这里只给出用户选择确定的界面,如果你选择返回那么知识下面的文字改变。

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();
     }

   confirm = new ConfirmationDialog(display, "继续操作嘛?");
     confirm.setDialogListener(this);
     confirm.display();
   }

   public void dialogDismissed(Dialog d, int code)
   {
     if (d == confirm)
     {

       if (code == ConfirmationDialog.YES)
       {
         message = new MessageDialog(display, "您选择了确定");
       } else
       {
         message = new MessageDialog(display, "您选择了返回");
       }
       message.display();
       message.setDialogListener(this);
     } else if (d == message)
     {

     Form f = new Form(null);
       f.append("退出程序");
       f.addCommand(exitCommand);
       f.setCommandListener(this);
       display.setCurrent(f);
     }
   }


}

上面的整个代码是一个完整的Dialog组件(不包括DialogTest)如果需要使用直接放在你的project里面就可以了!

(出处:http://www.cncms.com)


Tags:基于 MIDP 实现

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接