你需要一个
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调用.