在J2ME中,可以使用Yes&操作一个Alert对话框.没有命令? [英] In J2ME, is that possible to operate an Alert dialog box with Yes & NO command?

查看:101
本文介绍了在J2ME中,可以使用Yes&操作一个Alert对话框.没有命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在J2ME应用程序中创建了一个Alert对话框,以在用户按下退出按钮终止应用程序并要求用户确认时使用yes和no命令退出应用程序时向用户发出警报.

I have created an Alert dialog box in my J2ME app to alert user when user press exit button to terminate an app and ask user confirmation to exit from app with yes and no command.

当用户按是"时,按钮应用程序将终止,而当用户按否"按钮时,应用程序将返回其主要形式.为此,我从头开始编写了一个代码,如下所示:

When user press Yes button app will terminate and when user press No button app will return to its Main form. To do this I developed a code from scratch which are as follows:

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

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

In above code problem is when I click on Exit button, Alert box appears and when I press Yes button to terminate an app it again redirect to me on Main List of an app. I did lot of placements in code but problem remain constant.

对此有什么解决方案?

推荐答案

ExitAlert缺少命令侦听器,因为您没有为此调用setcommandListener.结果,发生了默认的命令操作,而不是预期的退出,它只是简单地消除了警报,如

ExitAlert in the posted code snippet lacks a command listener because you didn't invoke setcommandListener for it. As a result, instead of expected exit, default command action happens which is to simply dismiss the alert, as explained in API javadocs:

如果用户调用Command且存在默认侦听器,则默认侦听器将忽略Command并实现自动前进行为.

If the user invokes a Command and the default listener is present, the default listener ignores the Command and implements the automatic-advance behavior.

请注意,您可能会认为commandAction(Command cmd)方法中的ExitAlrt.setCommandListener(this)为您解决了问题,但事实并非如此,因为在创建ExitAlrt实例与显示该实例之间不会调用此方法.

Note you might think that ExitAlrt.setCommandListener(this) inside commandAction(Command cmd) method does the trick for you but this is not so, because this method is not invoked in between creation of the ExitAlrt instance and displaying it.

要获得所需的行为,请在调用setCurrent之前为ExitAlrt实现并设置适当的命令侦听器.

To get the desired behavior, implement and set an appropriate command listener for ExitAlrt prior to invoking 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问题,以获取有关如何以更实际的方式完成此操作的说明和示例.

For simplicity, above code snippet uses System.out.println for logging. If needed, refer to another SO question for an explanation and example of how this could be done in a more practical way.

这篇关于在J2ME中,可以使用Yes&操作一个Alert对话框.没有命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆