【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
        }
    }
相关推荐
2501_915918411 天前
深入对比iOS开发中常用性能监控工具的底层原理与优缺点分析
android·ios·小程序·https·uni-app·iphone·webview
my_power5201 天前
android中Activity生命周期函数的职责
android
Sirens.1 天前
从参考 iCost 到做自己的 OneLedger:一个 Android 本地记账 App 的开发记录
android·kotlin·room·jetpack compose·记账 app
qq_448011161 天前
C语言中的变量和函数的定义与声明
android·c语言·开发语言
码农coding1 天前
android 12 中的VSYNC的请求
android
阿pin1 天前
Android随笔-Activity
android·activity
YF02111 天前
详解Android所有文件访问权限
android
时间的拾荒人1 天前
MySQL C语言连接 - 从入门到实战
android·c语言·mysql
Meteors.1 天前
Android性能优化:01. 指标体系 + 分析工具链
android
jike_20261 天前
安卓平台免费录音转文字工具深度测评:5 款 APP 功能对比与选型指南
android·智能电视