1

我在我的 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();
        }
  }
}

在上面的代码问题中,当我单击退出按钮时,会出现警报框,当我按是按钮终止应用程序时,它会再次在应用程序的主列表中重定向到我。我在代码中做了很多放置,但问题仍然存在。

什么是解决方案?

4

1 回答 1

0

ExitAlert在发布的代码片段中缺少命令侦听器,因为您没有调用setcommandListener它。结果,不是预期的退出,而是发生默认命令操作,即简单地关闭警报,如API javadocs中所述:

如果用户调用命令并且存在默认侦听器,则默认侦听器会忽略命令并实现自动提前行为。

请注意,您可能认为ExitAlrt.setCommandListener(this)insidecommandAction(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用于记录。如果需要,请参阅另一个 SO 问题以获取有关如何以更实际的方式完成此操作的解释和示例。

于 2012-12-20T13:00:37.503 回答