Alert类用来给用户发出警告信息,但内容和类型不一定是警告性质的.可以把Alert认为是一个提供信息的对话框,其类型有:警告,错误,通知,确认等,显不Alert时用户界面会失去焦点.Alert可以自动定时解除,也可以设定一直保持在屏幕上让用户手动解除,(setTimeout(Alert.FOREVER)),解除后应用程序会继续下一个屏幕.
Alert类常用方法
| 类型 |
方法 |
说明 |
| void |
addCommand(Command cmd) |
增加命令项 |
| int |
getDefaultTimeout() |
获取默认的Alert解除时间 |
| Image |
getImage() |
获取Alert图标 |
| Gauage |
getIndicator() |
获取Alert的活动指示器 |
| String |
getString() |
获取Alert内的文本内容 |
| int |
getTimeout() |
获取Alert的解除时间 |
| AlertType |
getType() |
获取Alert的类型 |
| void |
removeCommand(Command com) |
移除命令项 |
| void |
setImage(Image img) |
设置Alert图标 |
| void |
setIndicator(Gauge indicator) |
设置Alert指标器 |
| void |
setString(String str) |
设置Alert显示内容 |
| void |
setTimeout(int time) |
设置Alert解除时间 |
| void |
setType(AlerType type) |
设置Alert类型 |
| void |
setCommandListener(Command listener) |
设置鉴听者 |
Alert构造函数
new Alert(String str) / new Alert(String title, String alertText, Image alertImage, AlertType alertType)
AlertType定义字段
| 类型 |
字段 |
说明 |
| static AlertType |
ALARM |
向用户警告一个事件 |
| static AlertType |
CONFIRMATION |
用来确认一个用户动作 |
| static AlertType |
ERROR |
向用户警告一个错误操作 |
| static AlertType |
INFO |
向用户提供一条一般性信息 |
| static AlertType |
WARING |
向用户警告一个危险操作 |
一个简单的例子
package demo; import javax.microedition.midlet.*; import javax.microedition.lcdui.*;
public class ExampleDemo extends MIDlet implements CommandListener { private Display display; private Form form; private Alert alert; private Command exit; private Command show;
public ExampleDemo() { display = Display.getDisplay(this); form = new Form("Alert 的例子"); alert = new Alert("Alert的标题","Alert里边的文字",null,AlertType.INFO);//设置一个Alert对象 alert.setTimeout(Alert.FOREVER);/*正如Alert.FOREVER字面意思一样Alet不会自动消失 ,当用户按了done时*/ exit = new Command("退出" , Command.EXIT, 1);//退出命令 show = new Command("显示" , Command.SCREEN,1);//显示Alert的命令 form.addCommand(exit); form.addCommand(show); form.setCommandListener(this); } public void startApp() { display.setCurrent(form); }
public void pauseApp() { }
public void destroyApp(boolean condition) {
}
public void commandAction(Command command ,Displayable displayable) { if(command == exit) { destroyApp(true); notifyDestroyed(); } if(command == show) { display.setCurrent(alert,form); }
} } |