问题
I wonder if there is any native support for andengine or ADK to ask question-toasts? For example if I press the back button, I want some box to popup asking if I really want to quit the application and give me the option to answer yes or no.
回答1:
Better to use alert dialog use this code, hope work same like that
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch(keyCode)
{
case KeyEvent.KEYCODE_BACK:
AlertDialog.Builder ab = new AlertDialog.Builder(AlertDialogExampleActivity.this);
ab.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();
break;
}
return super.onKeyDown(keyCode, event);
}
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
}
回答2:
You found your solution that is good but for the above answer you have to use AndEngine functionality. If you are working with AndEngine then you have to develop all the things with AndEngine power.
So for your solution you have to create one child scene which popup when user press device back button like in the following code snippet.
class DialogBox extends Scene{
DialogBox(...){
}
// you have to include all the functionality that your dialog box should contain
}
You have to set above dialog box as child of your main scene like in the following manner on the back event of user.
mScene.setAsChildScene(new DialogBox(...));
I prefer this way if I am developing a game.
来源:https://stackoverflow.com/questions/16872164/andengine-ask-questions-with-toast