ITeye博客

AlertType与Alert类

Jerke · 2011-01-25 10:40 · J2ME · 编程语言 · Markdown

AlertType类中有五个静态数据成员,这是预定义的AlertType类型,提供给Alert类使用。它们是:

(1)AlertType.ALARM 警报

(2)AlertType.CONFIRMATION 确认

(3)AlertType.ERROR 错误

(4)AlertType.INFO 提示信息

(5)AlertType.WARNING 警告

AlertType类中只有一个方法:boolean playSound(Display display),可以根据以上几种类型来发出声音。例如AlertType.ERROR.playSound(display)语句将发出“错误”类型的声音。

Alert类似于J2SE中的对话框,它有两个构造方法:

(1)Alert(String title)

(2)Alert(String title, String alertText, Image alertImage, AlertType alertType)

可以使用下面的方法来取得或设置构造方法中的参数:

String getString()/void setString(String str)
Image getImage()/void setImage(Image img)
AlertType getType()/void setType(AlertType type)

Alert实例的特征就是在显示一段时间后就会跳回原来的画面,这个时间间隔可以使用方法

void setTimeout(int time)类设置(如果使用Alert.FOREVER作为参数,那么它将一直显示直到用户按下解除键才消失),也可以使用方法int getTimeout()来获得。

Alert类还有一个静态数据成员Alert.DISMISS_COMMAND,它的作用是什么呢?如果没有使用addCommand(Command c)方法在Alert上加入系统菜单,那么Alert就会将Alert.DISMISS_COMMAND作为它的系统菜单,它传给commandAction(...)方法的第一个参数就是Alert.DISMISS_COMMAND;一旦有别的Command实例存在,Alert.DISMISS_COMMAND就消失了,它们是不共存的。

如果没有设定Alert事件的事件处理方法,那么不管是哪种菜单,画面都会回到上一页。

protected void showConfirmation(String title, String text, final int type) {

        Form form = new Form("");

        Alert alert = new Alert(title, text, null, AlertType.CONFIRMATION);

            alert.addCommand(new Command("Yes", Command.OK, 1));

            alert.addCommand(new Command("No", Command.CANCEL, 1));

        alert.setCommandListener(new CommandListener()    {

            public void commandAction(Command c, Displayable d) {

                if (c.getLabel().equals("Yes")) {

                    if (type == 0) {

                    } else if (type == 1) {

                    } else if (type == 2) {

                    }

                } else if (c.getLabel().equals("No")) {

                    gotoForm();

                } else if (c.getLabel().equals("确定")) {

                }

            }

        });

        Display.getDisplay(this).setCurrent(alert, form);

    }

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。