How to Prevent an Alert Dialog Getting Closed by Back Button

不问归期 提交于 2019-12-04 16:56:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!