开发者

How to Prevent an Alert Dialog Getting Closed by Back Button

I have an alert dialog like this:

    AlertDialog.Builder oyun开发者_如何学CaBaslaDialog = 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?


Use Dialog.setCancelable():

Sets whether this dialog is cancelable with the BACK key.

In your code this would be:

oyunaBaslaDialog.setCancelable(false);


Implement setOnKeyListener and catch the KeyEvent.KEYCODE_BACK. If you return true in this method, dialog will not close.


there is a method called, bulder.setCancelable().by default this cancelable set 'true'.override it with,

bulder.setCancelable(false)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜