【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
        }
    }
相关推荐
7ioik4 小时前
Explain关键字?
android
海盗12344 小时前
MySQL主从复制的配置方式
android·mysql·adb
liang_jy7 小时前
Android 事件分发机制(二)—— 点击事件透传
android·面试·源码
圆号本昊9 小时前
Flutter Android Live2D 2026 实战:模型加载 + 集成渲染 + 显示全流程 + 10 个核心坑( OpenGL )
android·flutter·live2d
冬奇Lab10 小时前
ANR实战分析:一次audioserver死锁引发的系统级故障排查
android·性能优化·debug
冬奇Lab10 小时前
Android车机卡顿案例剖析:从Binder耗尽到单例缺失的深度排查
android·性能优化·debug
ZHANG13HAO11 小时前
调用脚本实现 App 自动升级(无需无感、允许进程中断)
android
圆号本昊12 小时前
【2025最新】Flutter 加载显示 Live2D 角色,实战与踩坑全链路分享
android·flutter
小曹要微笑13 小时前
MySQL的TRIM函数
android·数据库·mysql