Android中focusableInTouchMode会导致第一次点击事件失效

我们很多时候会对某些View设置点击事件,但是,当对这个View同时设置了focusableInTouchMode=true时,第一次点击事件会被消费为为此View获取焦点。

xml 复制代码
<View
   	android:id="@+id/v_click"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    android:focusableInTouchMode="true"
    android:background="@color/cardview_dark_background"
    />

创建一个View,并同时指定了 android:focusableInTouchMode="true",在Activity代码中对View设置点击事件。

kotlin 复制代码
var count = 0

findViewById<View>(R.id.v_click)?.let {
     it.setOnFocusChangeListener { v, hasFocus -> Log.e("V_CLICK", "hasFocus  ${hasFocus}") }
     it.setOnClickListener {
         Log.e("V_CLICK", "click ${++count}")
     }
 }

发现第一次点击,LogCat显示:

bash 复制代码
V_CLICK  E  hasFocus  true

再次点击,才出现点击事件的Log:

bash 复制代码
V_CLICK  E  click 1
相关推荐
百***35512 分钟前
Tomcat10下载安装教程
java
Cola可洛24 分钟前
修复Flyme移植BUG
android·bug
一心只读圣贤猪25 分钟前
Canal ES Adapter pkVal 为 null 问题解决方案
java·后端
大头an31 分钟前
深入理解Spring核心原理:Bean作用域、生命周期与自动配置完全指南
java·后端
消失的旧时光-194344 分钟前
Kotlinx.serialization 使用指南
android·kotlin·json
戴誉杰1 小时前
idea 2025.2 重置试用30天,无限期使用
java·ide·intellij-idea
消失的旧时光-19432 小时前
Kotlinx.serialization 项目集成
android·kotlin·json
q***78782 小时前
Spring学习——新建module模块
java·学习·spring
q***11652 小时前
在Nginx上配置并开启WebDAV服务的完整指南
java·运维·nginx
白起那么早3 小时前
我又开发了一款idea插件-ContiNewGenerator
java·后端