允许 DialogFragment 的外部触摸 [英] Allow outside touch for DialogFragment

查看:24
本文介绍了允许 DialogFragment 的外部触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用中有一个 Fragment,它显示了一个 DialogFragment.
我在片段中有一个关闭对话框的按钮.但是当我显示 dialogFragment 时,对话框外部的触摸什么也不做,我无法单击对话框片段外部的按钮.

I have a Fragment in my app that shows a DialogFragment.
I have in the fragment a button that closes the dialog. But when I show the dialogFragment, the touches outside from the dialog do nothing and I can't click the buttons outside from the dialog fragment.

如何允许 DialogFragment 的外部触摸?

How can I allow outside touches for DialogFragment?

推荐答案

为了做到这一点,应该打开允许外部触摸的 Window 标志,并且为了获得良好的外观应清除背景暗淡标志.
由于必须在创建对话框后完成,所以我通过Handler 实现了它.

In order to do that, a flag of the Window that allows the outside touch should be turned on and for the good appearance the background dim flag should be cleared.
Since it must be done after dialog is created, I've implemented it via the Handler.

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // This is done in a post() since the dialog must be drawn before locating.
    getView().post(new Runnable() {

        @Override
        public void run() {

            Window dialogWindow = getDialog().getWindow();

            // Make the dialog possible to be outside touch
            dialogWindow.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
                    WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
            dialogWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

            getView().invalidate();
        }
    });
}

此时外线接触是可能的.

At this moment the outside touch is possible.

如果我们想让它更好看并且没有框架,可以添加以下代码:

In case we want to make it nicer and without the frame, the following code can be added:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Hide title of the dialog
    setStyle(STYLE_NO_FRAME, 0);
}

这篇关于允许 DialogFragment 的外部触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆