Java ME 中的是/否对话框

发布于 2024-07-04 07:21:06 字数 195 浏览 6 评论 0原文

我正在寻找一个简单的解决方案,用于在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花之痕靓丽 2024-07-11 07:21:06

您需要一个警报

警报是一个屏幕,向用户显示数据并等待一段时间,然后再继续下一个可显示的内容。 警报可以包含文本字符串和图像。 警报的预期用途是通知用户有关错误和其他异常情况的信息。

有 2 个 命令(“是“/”否“在你的情况下):

如果警报上存在两个或多个命令,则它会自动转换为模态警报,并且超时值始终为 FOREVER。 警报将一直显示在显示屏上,直到调用命令为止。

这些是 MIDP 1.0 及更高版本中支持的内置类。 而且你的代码片段永远不会工作。 这样的 API 需要阻止调用线程等待用户选择和应答。 这与基于回调和委托的 MIDP UI 交互模型完全相反。 您需要提供自己的类,实现 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 调用。

You need an 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.

With 2 commands ("Yes"/"No" in your case):

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.

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:

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
    }

};

To use it (again, untested and on top of my head):

MyPrompter prompt = new MyPrompter();
Display.getDisplay(YOUR_MIDLET_INSTANCE).setCurrent(prompt.getDisplayable());

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.

对风讲故事 2024-07-11 07:21:06

我没有用 Java ME 编程,但我在它的可选包参考中发现了
高级图形和用户界面 API,其使用方式与 Java SE API 类似使用 JOptionPane 类 创建这些对话框

int JOptionPane.showConfirmDialog(java.awt.Component ParentComponent, java.lang.Object >message, java.lang.String title, int optionType)

返回可以是
JOptionPane.YES_OPTIONJOptionPane.NO_OPTIONJOptionPane.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

int JOptionPane.showConfirmDialog(java.awt.Component parentComponent, java.lang.Object >message, java.lang.String title, int optionType)

Return could be
JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文