问题
I have an alert dialog like this:
AlertDialog.Builder oyunaBaslaDialog = new AlertDialog.Builder(this);
oyunaBaslaDialog.setMessage("A Takımı");
oyunaBaslaDialog.setNeutralButton("Başla!",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
oyunOyna();
}
});
oyunaBaslaDialog.show();
This dialog is shown in onCreate method. And I want it just to be closed by the button on it. But Hardware Back Button can also close this dialog without dialog's action performed.
I dont want the back button close this dialog, what can i do?
回答1:
Use Dialog.setCancelable():
Sets whether this dialog is cancelable with the BACK key.
In your code this would be:
oyunaBaslaDialog.setCancelable(false);
回答2:
Implement setOnKeyListener and catch the KeyEvent.KEYCODE_BACK. If you return true in this method, dialog will not close.
回答3:
there is a method called,
bulder.setCancelable().by default this cancelable set 'true'.override it with,
bulder.setCancelable(false)
来源:https://stackoverflow.com/questions/7113591/how-to-prevent-an-alert-dialog-getting-closed-by-back-button