源自:http://www.j2medev.com/Article/Class1/Class12/200803/5191.html)
下面的代码介绍了如何使用Alert来创建一个确认对话框。
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
public class ConfirmationMIDlet extends MIDlet implements CommandListener {
private Form form;
private Alert alert;
private Command exitCommand;
public void startApp() {
form = new Form("ConfirmationMIDlet");
exitCommand = new Command("Exit", Command.EXIT, 1);
form.setCommandListener(this);
form.addCommand(exitCommand);
Display.getDisplay(this).setCurrent(form);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public void commandAction (Command c, Displayable d) {
if (c == exitCommand) {
showConfirmation("Confirmation", "Do you really want to exit?");
}
}
private void closeAlert() {
Display.getDisplay(this).setCurrent(form);
alert = null;
}
protected void showConfirmation(String title, String text) {
alert = new Alert(title, text, null, AlertType.CONFIRMATION);
alert.addCommand(new Command ("Yes", Command.OK, 1));
alert.addCommand(new Command("No", Command.CANCEL, 1));
alert.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
if (c.getLabel().equals("Yes")) { notifyDestroyed();
}
if (c.getLabel().equals("No")) {
closeAlert();
}
}
});
Display.getDisplay(this).setCurrent(alert, form);
}
}
本文介绍了一个简单的Java ME程序,用于创建包含确认选项的对话框。该程序通过使用Alert类实现,能够显示带有“是”和“否”按钮的确认消息,并根据用户选择采取相应操作。
1195

被折叠的 条评论
为什么被折叠?



