【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
        }
    }
相关推荐
lxysbly1 小时前
安卓PCE模拟器下载指南:用手机玩 PC-Engine / TurboGrafx 经典
android·智能手机
Digitally3 小时前
哪款应用最适合将数据从安卓手机传输到 iPhone?
android·智能手机·iphone
Java小白,一起学习4 小时前
新版onenet云平台数据流对接,包括设备端MQTT和应用端API
android·物联网
走在路上的菜鸟4 小时前
Android学Flutter学习笔记 第一节 Android视角认知Flutter(View,intent,Async UI)
android·学习·flutter
一起搞IT吧4 小时前
相机Camera日志实例分析之十二:相机Camx【萌拍后置zoom拍照】单帧流程日志详解
android·c++·数码相机·智能手机
冬奇Lab5 小时前
一次 Android 车机黑屏问题的深度剖析:当显示驱动遇上中断风暴
android·性能优化·debug
兮动人5 小时前
Fatal error: Uncaught think\exception\ErrorException: SourceGuardian Loade
android·php
笔夏5 小时前
【安卓学习之myt】adb常用命令
android·学习·adb
lxysbly5 小时前
安卓gba模拟器下载
android
bst@微胖子5 小时前
CrewAI+FastAPI实现多Agent协作完成软件编码项目
android·fastapi