【Android】修复部分系统拖拽窗口导致弹窗消失的问题

问题

PopupWindow内部对外部事件进行了过滤,如果触发外部事件,会自动关闭弹窗

有些处理得不够好的系统,拖拽边缘时会被误认为是Outside事件来处理,这样就导致了弹窗关闭

方法

对TouchEvent进行拦截,过滤混淆事件

代码
kotlin 复制代码
    override fun setContentView(view: View) {
        super.setContentView(view)
        interceptOutsideEvent()
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun interceptOutsideEvent() {
        setTouchInterceptor { v, e ->
            val decorView = contentView.rootView
            if ((e.action == MotionEvent.ACTION_DOWN)) {
                val outside = e.x < 0 || e.x >= decorView.measuredWidth || e.y < 0 || e.y >= decorView.measuredHeight
                if (outside) {
                    ToastUtils.show("outside")
                    return@setTouchInterceptor true
                }
            } else if (e.action == MotionEvent.ACTION_OUTSIDE) {
                ToastUtils.show("outside")
                return@setTouchInterceptor true
            }
            return@setTouchInterceptor false
        }
    }
相关推荐
知行合一。。。3 小时前
Python--05--面向对象(属性,方法)
android·开发语言·python
leobertlan3 小时前
好玩系列:用20元实现快乐保存器
android·人工智能·算法
代龙涛6 小时前
WordPress single.php 文章模板开发详解
android
YF02118 小时前
Flutter 编译卡顿解决方案
android·flutter·ios
空中海8 小时前
第十二章:安卓实战架构与最佳实践
android·架构
angerdream10 小时前
Android手把手编写儿童手机远程监控App之四大组件详解2
android
kerli10 小时前
基于 kmp/cmp 的跨平台图片加载方案 - 适配 Android View/Compose/ios
android·前端·ios
半条-咸鱼11 小时前
基于安卓的 WAV 音频采集方案_含工具
android·音视频
九皇叔叔11 小时前
MySQL8.0 版本安装部署
android·adb