DialogFragment 点击区域外边缘不隐藏

最近在项目中发现一个现象,自定义的DialogFragment 出现后,你去点区域外的边缘,诶,怎么DialogFragment不会消失?非得点一定距离外的位置才会隐藏。这引起了我的好奇心。

一开始的猜测是:难道DialogFragment实际区域比可见区域大?然后我打开了debug.layout的开关。发现实际区域和可见区域是一致的,不存在padding。

在网上翻了一些帖子后,发现原来是它在搞鬼,如下Dialog源码中的onTouchEvent方法:

    /**
     * Called when a touch screen event was not handled by any of the views
     * under it. This is most useful to process touch events that happen outside
     * of your window bounds, where there is no view to receive it.
     * 
     * @param event The touch screen event being processed.
     * @return Return true if you have consumed the event, false if you haven't.
     *         The default implementation will cancel the dialog when a touch
     *         happens outside of the window bounds.
     */
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (mCancelable && mShowing && mWindow.shouldCloseOnTouch(mContext, event)) {
            cancel();
            return true;
        }
        
        return false;
    }

这个方法中会判断Window#showCloseOnTouch(Context context, MotionEvent event)。

我们继续往下跟:

    @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
    public boolean shouldCloseOnTouch(Context context, MotionEvent event) {
        final boolean isOutside =
                event.getAction() == MotionEvent.ACTION_UP && isOutOfBounds(context, event)
                || event.getAction() == MotionEvent.ACTION_OUTSIDE;
        if (mCloseOnTouchOutside && peekDecorView() != null && isOutside) {
            return true;
        }
        return false;
    }

一个值得怀疑的方法:isOutOfBounds 映入眼帘,继续往下跟:

    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));
    }

这里有个slop,呐,是它,没跑了。

我们看到,通过

left:-slop;

top:-slop;

right:decorView.getWidth()+slop;

bottom:decorView.getHeight()+slop;

构建了一个比Dialog视图更大的一个区域,只有点到这个区域外,Dialog才会dismiss。

既然原因找到了,那解决思路也有了,去掉这个slop的影响就行了。下面贴出解决方案:

1、自定义一个Dialog,重写onTouchEvent方法;

public class WithoutSlopDialog extends Dialog {

    private boolean mCancelable = true;//默认值是true,因为Dialog 默认是可以取消的

    public WithoutSlopDialog(@NonNull Context context) {
        super(context);
    }

    public WithoutSlopDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    public WithoutSlopDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    
    //重写此方法
    @Override
    public void setCanceledOnTouchOutside(boolean cancel) {
        super.setCanceledOnTouchOutside(cancel);
        mCancelable = cancel;
    }


    //重写onTouchEvent方法,用isOutOfBounds取代原本的Window#shouldCloseOnTouch(Context                     
    //context, MotionEvent event),从而消除slop的影响
    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        if (mCancelable && isShowing() && (event.getAction() == MotionEvent.ACTION_UP && isOutOfBounds(event)
                || event.getAction() == MotionEvent.ACTION_OUTSIDE)) {
            cancel();
            return true;
        }
        return super.onTouchEvent(event);
    }

    //看,没有slop了吧
    private boolean isOutOfBounds(MotionEvent event) {
        final int x = (int) event.getX();
        final int y = (int) event.getY();
        final View decorView = getWindow().getDecorView();
        return (x <= 0) || (y <= 0)
                || (x > (decorView.getWidth()))
                || (y > (decorView.getHeight()));
    }
}

2、在DialogFragment中重写onCreateDialog(Bundle savedInstanceState)方法,用我们自定义的Dialog来替换原生Dialog:

   @NonNull
   @Override
   public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
      return new WithoutSlopDialog(requireContext(), getTheme());
   }

至此,就解决啦。

DialogFragment的显示和隐藏可以通过调用show()和dismiss()方法来实现。在DialogFragment的show()方法中,会创建并显示Dialog。而在dismiss()方法中,会隐藏并销毁Dialog。 对于软键盘的显示和隐藏,可以通过在DialogFragment中使用InputMethodManager来实现。可以在DialogFragment的onCreateView()方法中给EditText设置点击监听,在点击EditText时调用showInput()方法来显示软键盘。在showInput()方法中,获取InputMethodManager实例,然后调用showSoftInput()方法将软键盘显示出来。 这里提供一个示例代码: private void showInput(View view) { InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { view.requestFocus(); imm.showSoftInput(view, 0); } } 然后在DialogFragment的onCreateView()方法中给EditText设置点击监听,如下所示: @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.dialog_fragment_layout, container, false); EditText editText = view.findViewById(R.id.edit_text); editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { showInput(v); } }); return view; } 这样,当EditText被点击时,就会调用showInput()方法显示软键盘。而在DialogFragment的dismiss()方法中,软键盘会自动隐藏掉。 参考资料: 参考资料1 参考资料2<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [dialogFragment软键盘隐藏后显示不出来的问题解决](https://blog.csdn.net/freak_csh/article/details/80781897)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Android dialog 去除虚拟按键的解决方法](https://download.csdn.net/download/weixin_38654944/12757041)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值