国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

基于MIDP實現Dialog組件

2019-11-18 16:13:46
字體:
來源:轉載
供稿:網友

    在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://m.survivalescaperooms.com)



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 南汇区| 长宁县| 米易县| 南和县| 乐山市| 贡嘎县| 沁水县| 罗定市| 沙湾县| 左云县| 济宁市| 德清县| 温州市| 大厂| 固原市| 柳林县| 堆龙德庆县| 新疆| 镇江市| 汉沽区| 深州市| 云阳县| 太仆寺旗| 久治县| 南投县| 泰宁县| 灵台县| 五大连池市| 芦溪县| 竹北市| 巴林右旗| 当涂县| 白银市| 定边县| 蚌埠市| 辽宁省| 清流县| 临沂市| 眉山市| 冕宁县| 措美县|