问题
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.
What is solution for this?
回答1:
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:
If the user invokes a Command and the default listener is present, the default listener ignores the Command and implements the automatic-advance behavior.
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.
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);
}
// ...
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.
来源:https://stackoverflow.com/questions/13971487/in-j2me-is-that-possible-to-operate-an-alert-dialog-box-with-yes-no-command