简体   繁体   English

Java ME中的是/否对话框

[英]Yes/No dialog in Java ME

I'm looking for a simple solution for a yes/no dialog to use in a Java ME midlet. 我正在寻找一个简单的解决方案,用于在Java ME midlet中使用yes / no对话框。 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
}

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. 警报是一个屏幕,向用户显示数据并等待一段时间后再继续显示下一个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. Alert的预期用途是告知用户错误和其他异常情况。

With 2 commands ("Yes"/"No" in your case): 使用2个命令 (在您的情况下为“是”/“否”):

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. 如果警报中存在两个或多个命令,它将自动转换为模态警报,并且超时值始终为FOREVER。 The Alert remains on the display until a Command is invoked. 警报将保留在显示屏上,直到调用Command。

These are built-in classes supported in MIDP 1.0 and higher. 这些是MIDP 1.0及更高版本支持的内置类。 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. 这样的API需要阻止等待用户选择和回答的调用线程。 This goes exactly in the opposite direction of the UI interaction model of MIDP, which is based in callbacks and delegation. 这恰好与MIDP的UI交互模型的方向相反,MIDP基于回调和委托。 You need to provide your own class, implementing CommandListener , and prepare your code for asynchronous execution. 您需要提供自己的类,实现CommandListener ,并为异步执行准备代码。

Here is an (untested!) example class based on Alert: 这是一个基于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. 您需要继续运行并等待commandAction调用。

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 我没有在Java ME中编程,但我在其中找到了可选包的高级图形和用户界面API ,它像Java SE API一样用于使用JOptionPane类创建这些对话框

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM