java android对话框_java - 防止Android活动对话框关闭外部触摸

这篇博客提供了多种防止Android活动对话框在用户点击后退键或触摸对话框外部时关闭的解决方案。包括使用`dialog.setCancelable(false)`和`dialog.setCanceledOnTouchOutside(false)`,以及在不同API级别下的适配方法,如`setFinishOnTouchOutside(false)`和针对API 11+的处理方式。
摘要由CSDN通过智能技术生成

18个解决方案

327 votes

要防止对话框在按下后退键时被解除,请使用此选项

dialog.setCancelable(false);

并且为防止对话框在外部触摸时被解雇,请使用此功能

dialog.setCanceledOnTouchOutside(false);

Singhak answered 2019-03-28T22:08:28Z

108 votes

你实际拥有的是一个Activity(即使它看起来像一个Dialog),因此如果你想在单击后台活动时保持它打开,你应该从你的活动中调用setFinishOnTouchOutside(false)。

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

artex answered 2019-03-28T22:08:59Z

39 votes

对我有用的是创建DialogFragment设置它不可取消:

dialog.setCancelable(false);

Petr answered 2019-03-28T22:09:23Z

31 votes

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

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

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

(PS:代码使用WindowManager.LayoutParams)

Michel-F. Portzert answered 2019-03-28T22:07:57Z

17 votes

对于更高的API 10,Dialog在触摸外部时消失,而在低于API 11时,Dialog不会消失。 为防止这种情况,您需要:

在styles.xml:onCreate()

要么

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

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

Andreyua answered 2019-03-28T22:10:12Z

11 votes

使用此代码它对我有用

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

alertDialog.setCancelable(false);

Aslam Patel answered 2019-03-28T22:10:40Z

11 votes

将对话框用作onCreate中的活动时添加此项

setFinishOnTouchOutside(false);

Ronny Shibley answered 2019-03-28T22:11:06Z

11 votes

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

dialog.setCanceledOnTouchOutside(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();

BillOverFlow answered 2019-03-28T22:11:51Z

9 votes

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

anand krish answered 2019-03-28T22:12:16Z

7 votes

只是,

alertDialog.setCancelable(false);

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

answered 2019-03-28T22:12:43Z

5 votes

使用setFinishOnTouchOutside(false)获取API> 11并且不要担心因为它的android的默认行为,活动主题对话框将无法在外部触摸上完成API< 11 :) !!干杯!

Vishal Pandey answered 2019-03-28T22:13:08Z

5 votes

我在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);

3c71 answered 2019-03-28T22:13:36Z

3 votes

alert.setCancelable(false);

alert.setCanceledOnTouchOutside(false);

我想这会对你有帮助。它为我工作

user2532065 answered 2019-03-28T22:14:01Z

3 votes

这是我的解决方案:

AlertDialog.Builder builder = new AlertDialog.Builder(this);

builder.setTitle("Select The Difficulty Level");

builder.setCancelable(false);

Akhter Alamin Farhan answered 2019-03-28T22:14:21Z

1 votes

我遇到了同样的问题。 为了处理它,我在对话框中设置了OntouchListener,并且什么也没做。 但是在旋转屏幕时Dialog也会被忽略。 为了解决这个问题,我设置了一个变量来告诉我对话框是否正常被解雇。 然后我在我的对话框中设置了一个OnDismissListener,在里面检查变量。 如果对话框通常不正常,我不会,或者我再次运行对话框(并设置他的状态,就像在我的情况下解雇)。

Madara answered 2019-03-28T22:14:48Z

1 votes

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

alertDialog.setOnCancelListener(new DialogInterface.OnCancelListener(){

@Override

public void onCancel(DialogInterface dialogInterface) {

//Your custom logic

}

});

Adae Rodríguez answered 2019-03-28T22:15:14Z

1 votes

builder.setCancelable(假);

public void 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();

}

-2 votes

这是您所有问题的完美答案....希望您喜欢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();

Akshat Rasogi answered 2019-03-28T22:16:14Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值