在对话框外按下时如何取消对话框片段?

我使用的是 DialogFragment,虽然我已经成功地设置了一个图像来关闭(即关闭)对话框时按下,我有一个困难的时间找到方法来关闭对话框时,用户点击它之外的任何地方,正如它的工作与正常的对话框。我还以为会有什么

dialogFragment.setCanceledOnTouchOutside(true);

但我在文件里没看到。

DialogFragment有可能做到这一点吗?还是我找错地方了?我尝试在“父母”活动中拦截触摸事件,但除了没有得到任何触摸事件,这似乎对我来说是不正确的。

53511 次浏览
DialogFragment.getDialog().setCanceledOnTouchOutside(true);

Must be called in onCreateView (as Apurv Gupta pointed out).

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
getDialog().setCanceledOnTouchOutside(true);
...
}

This works fine for Java:

DialogFragment.getDialog().setCanceledOnTouchOutside(false);
    /** The system calls this only when creating the layout in a dialog. */
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// The only reason you might override this method when using onCreateView() is
// to modify any dialog characteristics. For example, the dialog includes a
// title by default, but your custom layout might not need it. So here you can
// remove the dialog title, but you must call the superclass to get the Dialog.
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true);


return dialog;
}

I would recommend to use my solution only after trying out above solutions. I have described my solution here. Just to brief, I am checking touch bounds of DialogFragment.getView(). When touch points are outside DialogFragment, I am dismissing the Dialog.

            Dialog.SetCanceledOnTouchOutside(true);

Worked for me
My Code

class dlgRegister : DialogFragment
{
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
....
....
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
Dialog.SetCanceledOnTouchOutside(true);
base.OnActivityCreated(savedInstanceState);
Dialog.Window.Attributes.WindowAnimations =    Resource.Style.dialog_animation;
}
}

Lot of answers here but, the app crash when dialog opens. Writing getDialog().setCanceledOnTouchOutside(true); inside onCreateView did not work and crashed my app.

(I am using AppCompatActivity as my BaseActivity and android.app.DialogFragment as my Fragment).

What works is either of the two following lines:

getDialog().setCanceledOnTouchOutside(true);

OR

this.getDialog().setCanceledOnTouchOutside(true);

inside onActivityCreated like

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
//getDialog().getWindow().getAttributes().windowAnimations = R.style.DialogAnimationZoom;
//getDialog().getWindow().setDimAmount(0.85f);
getDialog().setCanceledOnTouchOutside(true);//See here is the code
}

What not to use:

DialogFragment.getDialog().setCanceledOnTouchOutside(false);

throws following error

enter image description here

And writing the code in onCreateView crashes the App! Please update the answer if you find something wrong.

If you want to execute some logic when clicking outside of a DialogFragment, just override the onCancel method.

override fun onCancel(dialog: DialogInterface) {
super.onCancel(dialog)
// Do your work here
}