1

J2MEアプリに[アラート]ダイアログボックスを作成しました。ユーザーが終了ボタンを押してアプリを終了し、yesおよびnoコマンドでアプリを終了することをユーザーに確認するように要求したときにユーザーにアラートを送信します。

ユーザーが[はい]ボタンを押すとアプリは終了し、ユーザーが[いいえ]ボタンを押すとアプリはメインフォームに戻ります。これを行うために、私は次のようなコードを最初から開発しました。

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)insideメソッドでうまくいくと思うかもしれませんが、そうではありません。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の質問を参照して、これをより実用的な方法で行う方法の説明と例を確認してください。

于 2012-12-20T13:00:37.503 に答える