J2ME学习笔记(3)--Alert类的学习

本文介绍了Alert类的基本用法,包括如何创建不同类型的Alert、设置显示内容和图标,以及如何通过设置命令来控制Alert的行为。提供了Alert类的方法和构造函数示例,并展示了如何在实际应用中使用这些功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Alert类的学习 

  Alert类用来给用户发出警告信息,但内容和类型不一定是警告性质的.可以把Alert认为是一个提供信息的对话框,其类型有:警告,错误,通知,确认等,显不Alert时用户界面会失去焦点.Alert可以自动定时解除,也可以设定一直保持在屏幕上让用户手动解除,(setTimeout(Alert.FOREVER)),解除后应用程序会继续下一个屏幕.

  Alert类常用方法

  

类型方法说明
void addCommand(Command cmd)增加命令项
intgetDefaultTimeout()获取默认的Alert解除时间
ImagegetImage()获取Alert图标
GauagegetIndicator()获取Alert的活动指示器
StringgetString()获取Alert内的文本内容
intgetTimeout()获取Alert的解除时间
AlertTypegetType()获取Alert的类型
voidremoveCommand(Command com)移除命令项
voidsetImage(Image img)设置Alert图标
voidsetIndicator(Gauge indicator)设置Alert指标器
voidsetString(String str)设置Alert显示内容
voidsetTimeout(int time)设置Alert解除时间
voidsetType(AlerType type)设置Alert类型
voidsetCommandListener(Command listener)设置鉴听者

Alert构造函数

new Alert(String str)   / new Alert(String title, String alertText, Image alertImage, AlertType alertType)

AlertType定义字段
类型字段说明
static AlertTypeALARM向用户警告一个事件
static AlertTypeCONFIRMATION用来确认一个用户动作
static AlertTypeERROR向用户警告一个错误操作
static AlertTypeINFO向用户提供一条一般性信息
static AlertTypeWARING向用户警告一个危险操作

一个简单的例子

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);
   
  }

 }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值