我有一个正在使用主题的活动。对话框样式,使其成为另一个活动上方的浮动窗口。但是,当我在对话框窗口外(在后台活动上)单击时,对话框关闭。我怎样才能阻止这种行为?


当前回答

对于API > 11使用setFinishOnTouchOutside(false),不要担心,因为它是android的默认行为,活动主题对话框不会在API < 11的外部触摸中完成:)!!

其他回答

对于API > 11使用setFinishOnTouchOutside(false),不要担心,因为它是android的默认行为,活动主题对话框不会在API < 11的外部触摸中完成:)!!

        alert.setCancelable(false);
        alert.setCanceledOnTouchOutside(false);

我想这对你有帮助。这对我很管用

当在onCreate中使用dialog作为一个活动时,添加这个

setFinishOnTouchOutside(false);

也可以分配不同的动作实现onCancelListener:

alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){                   
    @Override
    public void onCancel(DialogInterface dialogInterface) {
        //Your custom logic
    } 
});

这是对你所有问题的完美回答....希望你喜欢用Android编程

new AlertDialog.Builder(this)
            .setTitle("Akshat Rastogi Is Great")
            .setCancelable(false)
            .setMessage("I am the best Android Programmer")
            .setPositiveButton("I agree", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();

                }
            })
            .create().show();