Yes / No Dialog Box in Java ME

I am looking for a simple yes / no dialog solution for use in the Java ME midlet. I would 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
}
+5
source share
2 answers

You need Alert :

A warning is a screen showing data to the user and waiting for a certain period of time before moving on to the next one displayed. A warning may contain a text string and an image. The intended use of Alert is to inform the user of errors and other exceptional conditions.

2 ( "" / "" ):

Alert , , - . , .

, MIDP 1.0 . . API , . 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.

+7

Java ME, API , API Java SE JOptionPane

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

JOptionPane.YES_OPTION, JOptionPane.NO_OPTION, JOptionPane.CANCEL_OPTION...

-2

All Articles