首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> J2ME

MIDP Alert 類別完整應用指南:類型、超時、指示器與命令整合

     編輯:J2ME

Alert 核心特性

  • 模態對話框,顯示時取得焦點,父螢幕失去焦點。
  • 五種預設類型:ALARM、CONFIRMATION、ERROR、INFO、WARNING,對應預設圖示與聲音。
  • 可設定自動消失時間(毫秒)或 Alert.FOREVER 等待使用者確認。
  • 支援自訂圖片、Gauge 進度指示器、附加 Command。

建構子與常用方法

// 簡易建構子
Alert alert = new Alert("標題", "訊息內容", null, AlertType.INFO);

// 完整建構子
Alert alert = new Alert("標題", "訊息", image, AlertType.CONFIRMATION);

// 關鍵設定
alert.setTimeout(Alert.FOREVER);        // 永不自動關閉
alert.setTimeout(3000);                 // 3 秒後自動關閉
alert.setImage(myIcon);                 // 自訂圖示
alert.setIndicator(new Gauge(null,false,Gauge.INDEFINITE,Gauge.CONTINUOUS_RUNNING)); // 等待動畫
alert.addCommand(new Command("確定", Command.OK, 1));
alert.setCommandListener(this);

完整範例:確認對話框

public class AlertDemo extends MIDlet implements CommandListener {
    private Display display;
    private Form mainForm;
    private Alert confirmAlert;
    private Command cmdExit, cmdDelete, cmdAlertOK;

    public AlertDemo() {
        display = Display.getDisplay(this);
        mainForm = new Form("主選單");
        cmdExit = new Command("離開", Command.EXIT, 1);
        cmdDelete = new Command("刪除", Command.SCREEN, 1);
        mainForm.addCommand(cmdExit);
        mainForm.addCommand(cmdDelete);
        mainForm.setCommandListener(this);

        confirmAlert = new Alert("確認刪除", "確定要刪除所有資料嗎?", null, AlertType.CONFIRMATION);
        confirmAlert.setTimeout(Alert.FOREVER);
        cmdAlertOK = new Command("確定", Command.OK, 1);
        Command cmdAlertCancel = new Command("取消", Command.CANCEL, 2);
        confirmAlert.addCommand(cmdAlertOK);
        confirmAlert.addCommand(cmdAlertCancel);
        confirmAlert.setCommandListener(this);
    }

    public void startApp() { display.setCurrent(mainForm); }
    public void pauseApp() {}
    public void destroyApp(boolean u) {}

    public void commandAction(Command c, Displayable d) {
        if (c == cmdExit) { destroyApp(false); notifyDestroyed(); }
        else if (c == cmdDelete) { display.setCurrent(confirmAlert, mainForm); }
        else if (d == confirmAlert) {
            if (c == cmdAlertOK) { /* 執行刪除 */ }
            display.setCurrent(mainForm); // 取消或確定後回主選單
        }
    }
}

AlertType 選擇指南

類型適用情境預設行為
INFO一般提示(儲存完成、連線成功)無聲音、資訊圖示、預設 2-3 秒自動關閉
WARNING潛在風險(電量低、記憶體不足)警告聲、警告圖示
ERROR操作失敗(網路錯誤、檔案損毀)錯誤聲、錯誤圖示
ALARM鬧鐘、定時提醒鈴聲、鬧鐘圖示、通常 FOREVER
CONFIRMATION需使用者決定(刪除、覆蓋)問號圖示、FOREVER、搭配 OK/CANCEL Command

進階技巧

  • 自訂佈局:若預設 Alert 版面不符需求,改用 Form + setCommandListener 模擬對話框。
  • 多語系:標題、訊息、Command Label 皆從資源檔讀取。
  • 無障礙:搭配 AlertType 語意,螢幕閱讀器可正確播報。

常見錯誤

  1. 忘記 setCommandListener 導致按鈕無反應。
  2. 使用 display.setCurrent(alert) 而非 display.setCurrent(alert, nextScreen),關閉後畫面空白。
  3. 在 commandAction 中直接 notifyDestroyed() 而未先 destroyApp(),造成資源洩漏。

結語

Alert 是 MIDP 最輕量的互動元件,善用類型、超時與命令整合,能以最少代碼實作豐富的使用者確認流程。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved