Java ME 中的是/否对话框
我正在寻找一个简单的解决方案,用于在 Java ME midlet 中使用是/否对话框。 我想像这样使用它,但其他方式也可以。
if (YesNoDialog.ask("Are you sure?") == true) {
// yes was chosen
} else {
// no was chosen
}
I'm looking for a simple solution for a yes/no dialog to use in a Java ME midlet. I'd like to use it like this but other ways are okey.
if (YesNoDialog.ask("Are you sure?") == true) {
// yes was chosen
} else {
// no was chosen
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一个警报:
有 2 个 命令(“是“/”否“在你的情况下):
这些是 MIDP 1.0 及更高版本中支持的内置类。 而且你的代码片段永远不会工作。 这样的 API 需要阻止调用线程等待用户选择和应答。 这与基于回调和委托的 MIDP UI 交互模型完全相反。 您需要提供自己的类,实现 CommandListener ,并准备异步执行的代码。
这是一个基于 Alert 的(未经测试!)示例类:
要使用它(再次,未经测试且在我的脑海中):
此代码将使提示成为应用程序中当前显示的形式,但它不会像您发布的示例一样阻止您的线程。 您需要继续运行并等待 commandAction 调用。
You need an Alert:
With 2 commands ("Yes"/"No" in your case):
These are built-in classes supported in MIDP 1.0 and higher. Also your code snippet will never work. Such an API would need to block the calling thread awaiting for the user to select and answer. This goes exactly in the opposite direction of the UI interaction model of MIDP, which is based in callbacks and delegation. You need to provide your own class, implementing CommandListener, and prepare your code for asynchronous execution.
Here is an (untested!) example class based on Alert:
To use it (again, untested and on top of my head):
This code will make the prompt the current displayed form in your app, but it won't block your thread like in the example you posted. You need to continue running and wait for a commandAction invocation.
我没有用 Java ME 编程,但我在它的可选包参考中发现了
高级图形和用户界面 API,其使用方式与 Java SE API 类似使用 JOptionPane 类 创建这些对话框
返回可以是
JOptionPane.YES_OPTION
、JOptionPane.NO_OPTION
、JOptionPane.CANCEL_OPTION
...I dont have programed in Java ME, but i found in it's reference for optional packages the
Advanced Graphics and User Interface API, and it's used like the Java SE API to create these dialogs with the JOptionPane Class
Return could be
JOptionPane.YES_OPTION
,JOptionPane.NO_OPTION
,JOptionPane.CANCEL_OPTION
...