javax.microedition.lcdui.Alert 是 MIDP 高級 UI 中用於短暫信息提示、錯誤警告、確認對話框的特殊 Screen 子類。它具有自動播放提示音、自動超時返回、可選進度指示器等獨特行為。本文系統梳理 Alert 類型、屬性、生命周期、最佳實踐,並提供完整可運行示例。
| 特性 | 說明 |
|---|---|
| 繼承體系 | Screen → Displayable (可添加 Command、Ticker、CommandListener) |
| 顯示機制 | Display.setCurrent(alert) → 播放類型聲音 → 顯示內容 → 超時/用戶操作 → 自動返回前一 Screen |
| 前置條件 | **必須**在顯示 Alert 前設置好返回目標 Screen,否則拋出 IllegalStateException |
| 模態性 | 阻塞用戶交互,直到超時或用戶按鍵響應 |
AlertType 為不可實例化的工具類,提供 5 種標準類型,對應不同聲音與默認圖標:
| 類型常量 | 語義 | 典型場景 | 默認聲音 |
|---|---|---|---|
| ALARM | 警報 | 鬧鐘、緊急通知 | 響亮重複 |
| CONFIRMATION | 確認 | 操作成功、需用戶確認 | 輕快肯定 |
| ERROR | 錯誤 | 異常、操作失敗 | 刺耳警告 |
| INFO | 信息 | 一般提示、幫助信息 | 柔和提示 |
| WARNING | 警告 | 潛在風險、權限請求 | 注意提示 |
Alert alert = new Alert("標題"); // 或 new Alert("標題", "內容", null, AlertType.INFO)
// 核心設置
alert.setType(AlertType.ERROR); // 類型 (影響聲音/圖標)
alert.setString("詳細信息文本..."); // 主文本 (支持換行 \n)
alert.setImage(Image.createImage("/icon.png")); // 自定義圖標 (可選)
alert.setTimeout(3000); // 顯示毫秒數 (默認設備相關)
alert.setTimeout(Alert.FOREVER); // 永不超時,需用戶按鍵關閉
// 進度指示器 (超時倒計時視覺化)
Gauge gauge = new Gauge(null, false, 10, 0); // 最大值 10,初始 0
alert.setIndicator(gauge); // 關聯指示器
// 獲取器
AlertType type = alert.getType();
String text = alert.getString();
Image img = alert.getImage();
int timeout = alert.getTimeout();
Gauge indicator = alert.getIndicator();
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
public class AlertDemoMIDlet extends MIDlet implements CommandListener {
private Display display;
private Form configForm;
private ChoiceGroup typeChoice, timeoutChoice, optionChoice;
private static final Command CMD_SHOW = new Command("顯示", Command.OK, 1);
private static final Command CMD_EXIT = new Command("退出", Command.EXIT, 2);
private static final String[] TYPES = {"ALARM", "CONFIRMATION", "ERROR", "INFO", "WARNING"};
private static final AlertType[] TYPE_VALUES = {
AlertType.ALARM, AlertType.CONFIRMATION, AlertType.ERROR, AlertType.INFO, AlertType.WARNING
};
private static final String[] TIMEOUTS = {"2秒", "5秒", "10秒", "永久 (FOREVER)"};
private static final int[] TIMEOUT_VALUES = {2000, 5000, 10000, Alert.FOREVER};
protected void startApp() {
if (display == null) display = Display.getDisplay(this);
if (configForm == null) buildConfigForm();
display.setCurrent(configForm);
}
private void buildConfigForm() {
configForm = new Form("Alert 配置");
typeChoice = new ChoiceGroup("類型", Choice.POPUP, TYPES, null);
timeoutChoice = new ChoiceGroup("超時", Choice.POPUP, TIMEOUTS, null);
optionChoice = new ChoiceGroup("選項", Choice.MULTIPLE, new String[]{"顯示進度指示器"}, null);
configForm.append(typeChoice);
configForm.append(timeoutChoice);
configForm.append(optionChoice);
configForm.addCommand(CMD_SHOW);
configForm.addCommand(CMD_EXIT);
configForm.setCommandListener(this);
}
public void commandAction(Command c, Displayable d) {
if (c == CMD_EXIT) { notifyDestroyed(); return; }
if (c != CMD_SHOW) return;
int typeIdx = typeChoice.getSelectedIndex();
int timeoutIdx = timeoutChoice.getSelectedIndex();
boolean showIndicator = optionChoice.isSelected(0);
Alert alert = new Alert("演示 Alert");
alert.setType(TYPE_VALUES[typeIdx]);
alert.setTimeout(TIMEOUT_VALUES[timeoutIdx]);
alert.setString(TYPES[typeIdx] + " 類型\n超時: " + TIMEOUTS[timeoutIdx]);
if (showIndicator) {
Gauge indicator = createIndicator(TIMEOUT_VALUES[timeoutIdx]);
alert.setIndicator(indicator);
}
// 關鍵:設置返回目標 (當前配置表單)
display.setCurrent(alert, configForm);
}
private Gauge createIndicator(int timeout) {
if (timeout == Alert.FOREVER) {
// 無限進度條 (動畫)
return new Gauge(null, false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
}
// 定時進度條:最大值 = 秒數,啟動線程每秒更新
final int seconds = timeout / 1000;
final Gauge gauge = new Gauge(null, false, seconds, 0);
new Thread(() -> {
for (int i = 0; i < seconds; i++) {
gauge.setValue(i);
try { Thread.sleep(1000); } catch (InterruptedException ignored) {}
}
}).start();
return gauge;
}
protected void pauseApp() {}
protected void destroyApp(boolean unconditional) {}
}
setCurrent(alert, nextScreen):顯示 Alert,超時/關閉後自動切換到 nextScreentimeout > 0:倒計時結束自動關閉,返回 nextScreentimeout == FOREVER:一直顯示,直到用戶按鍵 (通常是軟鍵「確定」/「返回」)Command 且超時:超時會自動激活該 Command (僅當只有一個 Command 時)CommandListener (若有 Command),然後返回 nextScreen| 場景 | 建議 | 錯誤示範 |
|---|---|---|
| 網絡請求失敗提示 | ERROR 類型 + 3-5 秒超時 + 關鍵錯誤碼 | FOREVER 導致用戶卡死,無法返回重試 |
| 表單提交成功確認 | CONFIRMATION + 2 秒 + 自動返回列表 | 需用戶手動關閉,增加操作步驟 |
| 關鍵權限請求 | WARNING + FOREVER + 兩個 Command (允許/拒絕) | 無 Command 用戶無法響應 |
| 長時間後台任務進度 | INFO + FOREVER + Gauge.INDEFINITE + 取消 Command | 無進度指示用戶以為死機 |
| 文本過長 | 簡潔核心信息,詳細放入後續 Form | 塞入大段文本,小屏幕截斷/換行混亂 |
| 圖標加載失敗 | try-catch 包裹 createImage,失敗設 null | 拋出異常導致 Alert 無法顯示 |
Alert 是 MIDP 中唯一自帶聲音、自動超時返回、內置進度指示器的高級組件。掌握類型選擇、超時策略、返回目標設置、指示器配置四大要素,即可構建良好的用戶交互反饋體驗。記住鐵律:顯示 Alert 前必須確定返回 Screen,否則運行時崩潰。