【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 小时前
跨平台开发的抉择:Flutter vs 原生安卓(Kotlin)的优劣对比与选型建议
android·flutter·kotlin
jyan_敬言9 小时前
【C++】string类(二)相关接口介绍及其使用
android·开发语言·c++·青少年编程·visual studio
程序员老刘9 小时前
Android 16开发者全解读
android·flutter·客户端
福柯柯10 小时前
Android ContentProvider的使用
android·contenprovider
不想迷路的小男孩10 小时前
Android Studio 中Palette跟Component Tree面板消失怎么恢复正常
android·ide·android studio
餐桌上的王子10 小时前
Android 构建可管理生命周期的应用(一)
android
菠萝加点糖10 小时前
Android Camera2 + OpenGL离屏渲染示例
android·opengl·camera
用户20187928316710 小时前
🌟 童话:四大Context徽章诞生记
android
yzpyzp10 小时前
Android studio在点击运行按钮时执行过程中输出的compileDebugKotlin 这个任务是由gradle执行的吗
android·gradle·android studio
aningxiaoxixi11 小时前
安卓之service
android