在J2ME中,是可以操作与是与NO命令警告对话框?(In J2ME, is that possibl

2019-10-17 12:55发布

我在J2ME应用程序创建的警报对话框,提醒用户,当用户按EXIT终止应用程序,并要求用户确认从应用程序退出与yes和no命令。

当用户按下Yes按钮的应用程序将终止,当用户按下否按钮的应用程序将返回到它的主要形式。 要做到这一点,我开发了一个从无到有的代码如下:

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

在上面的代码中的问题是,当我点击退出按钮,警告框出现,当我按下Yes按钮来终止应用程序再次重定向到我的应用程序的主列表。 我做了很多的代码,但问题的展示位置保持不变。

什么是解决这个?

Answer 1:

ExitAlert在发布代码片段缺少一个命令监听,因为你没有援引setcommandListener它。 作为结果,而不是期望的退出,默认命令动作发生这是简单地解除警报,在解释API的javadoc :

如果用户调用一个命令和所述缺省侦听器存在,则缺省侦听器忽略该命令,并实现了自动前移的行为。

请注意,您可能会认为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用于记录。 如果需要,请参阅另一个SO问题,对于如何能在一个更实际的方式进行解释和例子。



文章来源: In J2ME, is that possible to operate an Alert dialog box with Yes & NO command?