1. gzyueqian
      13424082685
      首頁 > 新聞中心 > > 正文

      基于MIDP實現Dialog組件

      更新時間: 2007-01-19 09:46:21來源: 粵嵌教育瀏覽量:748

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

          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。首先我們看一下它的截圖然后給出源代碼!
       
        這里只給出用戶選擇確定的界面,如果你選擇返回那么知識下面的文字改變。

      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里面就可以了!

      免費預約試聽課

      亚洲另类欧美综合久久图片区_亚洲中文字幕日产无码2020_欧美日本一区二区三区桃色视频_亚洲AⅤ天堂一区二区三区

      
      

      1. 午夜福利国产视频 | 亚洲欧美久久一区二区 | 中文字幕天堂手机版 | 午夜免费啪视频在线观看区 | 亚洲图片中文字幕 | 日本午夜视频黄 |