257

我有一个使用 Theme.Dialog 样式的活动,因此它是另一个活动上方的浮动窗口。但是,当我在对话框窗口外(在后台活动上)单击时,对话框将关闭。我怎样才能阻止这种行为?

4

19 回答 19

513

要防止对话框在按下后退键时消失,请使用此

dialog.setCancelable(false);

并防止对话框在外部触摸时被关闭,请使用此

 dialog.setCanceledOnTouchOutside(false);
于 2013-09-28T08:28:08.080 回答
127

您实际上拥有的是一个 Activity(即使它看起来像一个对话框),因此setFinishOnTouchOutside(false)如果您想在单击背景 Activity 时保持它处于打开状态,则应该从您的 Activity 中调用。

编辑:这仅适用于 android API 级别 11 或更高版本

于 2013-04-03T15:18:56.307 回答
81

对我有用的是创建DialogFragment一个不可取消的集合:

dialog.setCancelable(false);
于 2013-08-01T19:45:21.843 回答
34

这可以帮助你。这是一种处理外部触摸事件的方法:

在窗口外触摸时如何取消以 Activity 为主题的对话框?

通过捕捉事件并且什么都不做,我认为你可以防止关闭。但奇怪的是,当您触摸外部时,活动对话框的默认行为不应该是自行关闭。

(PS:代码使用WindowManager.LayoutParams)

于 2012-08-24T03:32:08.863 回答
22

当在 onCreate 中使用对话框作为活动时,添加这个

setFinishOnTouchOutside(false);
于 2017-04-01T10:05:47.530 回答
18

对于更高的 API 10,当在外部触摸时对话框会消失,而在低于 API 11 的情况下,对话框不会消失。为了防止这种情况,您需要执行以下操作:

styles.xml<item name="android:windowCloseOnTouchOutside">false</item>

或者

onCreate()方法中,使用:this.setFinishOnTouchOutside(false);

注意:对于 API 10 及更低版本,此方法无效,不需要。

于 2014-04-20T22:00:01.340 回答
17

将可取消的对话框设置为 false 就足够了,您在警报对话框之外触摸或单击后退按钮都会使警报对话框消失。所以使用这个:

setCancelable(false)

并且不再需要其他功能: dialog.setCanceledOnTouchOutside(false);

如果您正在创建一个临时对话框并想在那里放置这行代码,这里有一个示例:

new AlertDialog.Builder(this)
                        .setTitle("Trial Version")
                        .setCancelable(false)
                        .setMessage("You are using trial version!")
                        .setIcon(R.drawable.time_left)
                        .setPositiveButton(android.R.string.yes, null).show();
于 2015-11-04T02:42:41.023 回答
14
Dialog dialog = new Dialog(context)
dialog.setCanceledOnTouchOutside(true); 
//use this to dismiss the dialog on outside click of dialog

dialog.setCanceledOnTouchOutside(false);
//use this for not to dismiss the dialog on outside click of dialog.

观看此链接以获取有关对话框的更多详细信息。

dialog.setCancelable(false);
//used to prevent the dismiss of dialog on backpress of that activity

dialog.setCancelable(true);
//used to dismiss the dialog on onbackpressed of that activity
于 2015-02-06T09:20:15.737 回答
13

使用此代码对我有用

 AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
 alertDialog.setCancelable(false);

于 2016-01-02T09:51:19.163 回答
13

不推荐使用警报对话框,因此请使用 Dialog dialog = new Dialog(this);

为了防止关闭外部触摸

dialog.setCanceledOnTouchOutside(false);
于 2019-10-05T08:29:53.860 回答
10

简单地,

alertDialog.setCancelable(false);

防止用户在对话框外单击。

于 2017-05-19T18:45:06.427 回答
6

我在 onCreate() 中使用它,似乎适用于任何版本的 Android;在 5.0 和 4.4.x 上测试,无法在 Gingerbread 上测试,三星设备(运行 GB 的注 1)默认采用这种方式:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
    {
        setFinishOnTouchOutside(false);
    }
    else
    {
        getWindow().clearFlags(LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH);
    }

    super.onCreate(savedInstanceState);
于 2014-12-16T19:34:54.340 回答
5

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

于 2014-08-25T03:45:41.427 回答
5
        alert.setCancelable(false);
        alert.setCanceledOnTouchOutside(false);

我想这会对你有所帮助。它对我有用

于 2015-09-14T02:38:58.210 回答
3

这是我的解决方案:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select The Difficulty Level");
builder.setCancelable(false);
于 2016-04-12T04:37:32.430 回答
2

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

alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){                   
    @Override
    public void onCancel(DialogInterface dialogInterface) {
        //Your custom logic
    } 
});
于 2015-09-21T14:33:07.627 回答
1

我面临着同样的问题。为了处理它,我OntouchListener在对话框中设置了一个并且在里面什么都不做。但是旋转屏幕时对话框也会消失。为了修复它,我设置了一个变量来告诉我对话框是否正常关闭。然后我OnDismissListener在我的对话框中设置 a 并在里面检查变量。如果对话框正常关闭,我什么也不做,否则我再次运行对话框(并将他的状态设置为在我的情况下关闭时)。

于 2014-02-06T15:47:46.510 回答
1

builder.setCancelable(false);


公共无效 Mensaje(查看 v){

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("¿Quieres ir a el Menú principal?");
    builder.setMessage("Al presionar SI iras a el menú y saldras de la materia.");
    builder.setPositiveButton("SI", null);
    builder.setNegativeButton("NO", null);
    builder.setCancelable(false);
    builder.show();
}
于 2016-07-12T23:24:32.833 回答
-2

这是您所有问题的完美答案......希望您喜欢在 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();
于 2018-04-22T07:30:51.000 回答