You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android(Kotlin)中如何禁用View的触摸事件?

Android引导页禁用全局触摸(仅保留Tooltip交互)解决方案
  • 添加全屏遮罩拦截View
    在Activity或Fragment的根布局最上层添加一个全屏透明View,用来覆盖所有原有控件,拦截默认触摸事件。布局需确保遮罩层级最高:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <!-- 原有页面内容布局 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- 你的业务控件 -->
        </LinearLayout>
    
        <!-- 触摸拦截遮罩 -->
        <View
            android:id="@+id/touchBlockerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:clickable="true"
            android:focusable="true" />
    </FrameLayout>
    
  • 自定义触摸过滤逻辑
    给遮罩View设置触摸监听,仅允许Tooltip区域的触摸事件透传,其余全部拦截:

    val touchBlockerView = findViewById<View>(R.id.touchBlockerView)
    touchBlockerView.setOnTouchListener { _, event ->
        // 获取Tooltip的实际显示View
        val tooltipView = // 替换为你的Tooltip实例对应的View
        val tooltipRect = Rect()
        tooltipView.getGlobalVisibleRect(tooltipRect)
    
        // 判断触摸点是否落在Tooltip范围内
        val isTouchOnTooltip = tooltipRect.contains(event.rawX.toInt(), event.rawY.toInt())
        !isTouchOnTooltip // 非Tooltip区域返回true拦截事件,反之允许透传
    }
    

    若使用系统TooltipCompat或第三方Tooltip库,需注意获取其Window层面的实际View实例,可通过WindowManager遍历查找对应类型的View。

  • 无遮罩替代方案:重写Activity触摸分发
    无需额外布局,直接在Activity中全局拦截触摸事件:

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        val tooltipView = // 获取你的Tooltip显示View
        val tooltipRect = Rect()
        tooltipView.getGlobalVisibleRect(tooltipRect)
        val isTouchOnTooltip = tooltipRect.contains(ev.rawX.toInt(), ev.rawY.toInt())
        return if (isTouchOnTooltip) {
            super.dispatchTouchEvent(ev) // 放行Tooltip触摸
        } else {
            true // 拦截所有其他触摸
        }
    }
    
  • 关键注意事项

    • 确保Tooltip已初始化并显示后再执行触摸拦截,避免getGlobalVisibleRect返回空
    • 动态控制遮罩的显示/隐藏:Tooltip弹出时启用拦截,关闭时解除拦截
    • 多Fragment场景建议在Activity层面处理,避免Fragment间交互冲突

内容的提问来源于stack exchange,提问作者Iñigo Bereciartua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 18:25:34