java 对话框 是 否_Java ME中的是/否对话框

你需要一个

Alert:

An alert is a screen that shows data to the user and waits for a certain period of time before proceeding to the next Displayable. An alert can contain a text string and an image. The intended use of Alert is to inform the user about errors and other exceptional conditions.

使用2 commands(在您的情况下为“是”/“否”):

If there are two or more Commands present on the Alert, it is automatically turned into a modal Alert, and the timeout value is always FOREVER. The Alert remains on the display until a Command is invoked.

这些是MIDP 1.0及更高版本支持的内置类.您的代码段也永远不会有效.这样的API需要阻止等待用户选择和回答的调用线程.这恰好与MIDP的UI交互模型的方向相反,MIDP基于回调和委托.您需要提供自己的类,实现CommandListener,并准备代码以进行异步执行.

这是一个基于Alert的(未经测试的!)示例类:

public class MyPrompter implements CommandListener {

private Alert yesNoAlert;

private Command softKey1;

private Command softKey2;

private boolean status;

public MyPrompter() {

yesNoAlert = new Alert("Attention");

yesNoAlert.setString("Are you sure?");

softKey1 = new Command("No", Command.BACK, 1);

softKey2 = new Command("Yes", Command.OK, 1);

yesNoAlert.addCommand(softKey1);

yesNoAlert.addCommand(softKey2);

yesNoAlert.setCommandListener(this);

status = false;

}

public Displayable getDisplayable() {

return yesNoAlert;

}

public boolean getStatus() {

return status;

}

public void commandAction(Command c, Displayable d) {

status = c.getCommandType() == Command.OK;

// maybe do other stuff here. remember this is asynchronous

}

};

使用它(再次,未经测试,在我的头顶):

MyPrompter prompt = new MyPrompter();

Display.getDisplay(YOUR_MIDLET_INSTANCE).setCurrent(prompt.getDisplayable());

此代码将在应用程序中提示当前显示的表单,但不会像您发布的示例中那样阻止您的线程.您需要继续运行并等待commandAction调用.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值