android - 单击靠近其边缘时,dialogFragment 不会关闭

标签 android dialogfragment

我有一个自定义的dialogFragment,它基本上可以工作,但是只有当您在远离对话框的地方单击(即点击)时它才会消失。如果我在非常靠近对话框的地方单击,但仍在外部(例如距离边缘 30 像素),则不会发生任何事情...对话框不会关闭。

我发现即使在不使用自定义的基本警报对话框上也会发生这种情况。据我所知,这是一个标准的 Android 东西。我错了吗?有什么原因吗?

enter image description here

有一个属性.setCanceledOnTouchOutside();更改确实会影响按预期进行的远距离点击消除,但对上述接近边缘的情况没有影响。

对话框类:

public class Filters_DialogFragment extends DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.filters_dialog, container, false);
        getDialog().setTitle("Simple Dialog");

        // FYI, this has no affect on clicking very close to the dialog edge.
        getDialog().setCanceledOnTouchOutside(true);

        return rootView;
    }

}

对话框布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#333333">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="FILTERS"
        android:textColor="#ffffff" />

    <SeekBar
        android:id="@+id/seekBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

我的 Activity 中调用对话框的函数:

private void showFiltersDialog() {

    FragmentManager fm = getSupportFragmentManager();
    Filters_DialogFragment dialogFragment = new Filters_DialogFragment();
    dialogFragment.show(fm, "Sample Fragment");

}

最佳答案

我自己也遇到过这个问题,因此对源代码进行了一些挖掘。事实证明这是有意的行为,被称为“touchSlop”。它在 ViewConfiguration 中定义:

https://developer.android.com/reference/android/view/ViewConfiguration.html#getScaledWindowTouchSlop()

有问题的代码位于 Window 类中:

public boolean shouldCloseOnTouch(Context context, MotionEvent event) {
        if (mCloseOnTouchOutside && event.getAction() == MotionEvent.ACTION_DOWN
                && isOutOfBounds(context, event) && peekDecorView() != null) {
            return true;
        }
        return false;
    }

然后调用:

private boolean isOutOfBounds(Context context, MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final int slop = ViewConfiguration.get(context).getScaledWindowTouchSlop();
        final View decorView = getDecorView();
        return (x < -slop) || (y < -slop)
                || (x > (decorView.getWidth()+slop))
                || (y > (decorView.getHeight()+slop));
    }

其值(value)来自:

/**
     * Distance in dips a touch needs to be outside of a window's bounds for it to
     * count as outside for purposes of dismissing the window.
     */
    private static final int WINDOW_TOUCH_SLOP = 16;

我找不到任何方法来覆盖此行为或更改斜率值。我认为唯一的选择是实现具有透明背景和手动单击处理程序的全屏对话框。我认为我的应用程序覆盖默认系统行为不是一个好主意,因此我不打算实现它。

关于android - 单击靠近其边缘时,dialogFragment 不会关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42798780/

相关文章:

android - 房间数据库删除查询

java - 在 Activity 对象的帮助下访问 RecyclerAdapter 类内的 FragmentManager 时出现 IllegalStateException

android - OnItemClickListener 和自定义 OnTouchListener

php - Android JSON 和 php

java - 我的布局标记使用带有 ImageView 和 TextView 的 ScrollView 移动

android - 在回收站 View 中滚动隐藏/显示时查看闪烁

Android 无需重启即可在应用内切换语言

java - 全屏 DialogFragment 与 StatusBar 重叠

android - 如何检查自定义DialogFragment是否显示?

java - 将数据从 DialogFragment 传递回其调用 Activity ?