我在我的J2ME应用程序中创建了一个警告对话框,当用户按下退出按钮终止某个应用程序并要求用户确认使用是和否命令退出应用程序时提醒用户。
当用户按是按钮应用程序将终止,当用户按下否按钮应用程序将返回其主窗体。为此,我从头开始编写如下代码:
public class CustomAlert extends MIDlet implements CommandListener
{
Alert ExitAlrt;
Display d;
Command MainListSelect, Exit, YesCmdAlrt, NoCmdAlrt;
List MainList;
public CustomAlert()
{
d = Display.getDisplay(this);
//Initialization of commands
MainListSelect = new Command("Select", Command.SCREEN, 1);
Exit = new Command("Exit", Command.STOP, 2);
//Initialization of lists
MainList = new List("Menu", List.IMPLICIT);
//Adding command to lists
MainList.addCommand(MainListSelect);
MainList.addCommand(Exit);
MainList.setCommandListener(this);
//Appending the content of lists
MainList.append("Settings",null);
}
protected void startApp()
{
MainList.setSelectedIndex(0, true);
d.setCurrent(MainList);
}
protected void pauseApp() { }
protected void destroyApp(boolean unconditional){}
//This method handle commands which operate list that is Select & Exit
public void commandAction(Command cmd,Displayable dispable)
{
if(cmd == MainListSelect)
{
int slctindx = MainList.getSelectedIndex();
if(slctindx == 0)
{}
else if(slctindx == 1)
{}
}
if(cmd == Exit)
{
ExitAlrt = new Alert("Application Alert","Are you sure you want to exit?",null, AlertType.WARNING);
YesCmdAlrt = new Command("Yes", Command.EXIT,1);
ExitAlrt.addCommand(YesCmdAlrt);
NoCmdAlrt = new Command("No", Command.SCREEN,2);
ExitAlrt.addCommand(NoCmdAlrt);
d.setCurrent(ExitAlrt);
}
}
//This Code handle Commands present on Alert dialog box.
public void commandAction(Command cmd) /
{
ExitAlrt.setCommandListener(this);
if(cmd == NoCmdAlrt)
{
d.setCurrent(MainList);
}
else if(cmd == YesCmdAlrt)
{
destroyApp(true);
notifyDestroyed();
}
}
}
在上面的代码问题是,当我点击退出按钮,出现警告框,当我按是按钮终止应用程序时,它再次重定向到应用程序的主列表上的我。我在代码中做了很多放置,但问题仍然存在。
这是什么解决方案?
答案 0 :(得分:0)
ExitAlert
缺少命令侦听器,因为您没有为其调用setcommandListener
。因此,正如API javadocs中所解释的那样,默认命令操作只是简单地解除警报,而不是预期的退出,而不是预期的退出:
如果用户调用Command并且存在默认侦听器,则默认侦听器将忽略Command并实现自动前进行为。
请注意,您可能认为ExitAlrt.setCommandListener(this)
方法中的commandAction(Command cmd)
为您提供了技巧,但事实并非如此,因为在创建ExitAlrt
实例和显示之间不会调用此方法它
要获得所需的行为,请在调用ExitAlrt
之前为setCurrent
实现并设置适当的命令侦听器。
// ...
if(cmd == Exit)
{
System.out.println("Exit command invoked"); // log message for debugging
Alert ExitAlrt = new Alert("Application Alert",
"Are you sure you want to exit?", null, AlertType.WARNING);
ExitAlrt.addCommand(new Command("Yes", Command.EXIT, 1));
ExitAlrt.addCommand(new Command("No", Command.SCREEN, 2));
// --> set command listener for ExitAlrt prior to invoking setCurrent
ExitAlrt.setCommandListener(new CommandListener() {
public void commandAction(Command c, Displayable d) {
System.out.println("command: [" + c.getCommandLabel()
+ "] at screen: [" + d.getTitle() + "]"); // for debugging
if (c.getCommandType() != Command.EXIT) {
System.out.println("Exit cancelled"); // for debugging
d.setCurrent(MainList);
return;
}
System.out.println("Exit confirmed"); // for debugging
destroyApp(true);
notifyDestroyed();
}
});
d.setCurrent(ExitAlrt);
}
// ...
为简单起见,上面的代码段使用System.out.println
进行日志记录。如果需要,请参阅another SO question以获取解释以及如何以更实际的方式完成此操作的示例。