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
相关推荐
零雲1 分钟前
java面试:怎么保证消息队列当中的消息丢失、重复问题?
java·开发语言·面试
冬夜戏雪1 分钟前
【java学习日记】【12.11】【11/60】
java·开发语言
用户2190326527352 分钟前
实现Spring Cloud Sleuth的Trace ID追踪日志实战教程
java·后端
vx_bisheyuange2 分钟前
基于SpringBoot的在线互动学习网站设计
java·spring boot·spring·毕业设计
roman_日积跬步-终至千里2 分钟前
【源码分析】StarRocks TRUNCATE 语句执行流程:从 SQL 到数据清空的完整旅程
java·数据库·sql
雨中飘荡的记忆4 分钟前
Spring状态机深度解析:从入门到生产实战
java·spring
Kings905 分钟前
线程池导致的 shutdown失败的完整排查过程
java·spring boot
在坚持一下我可没意见6 分钟前
Spring 后端安全双剑(下篇):JWT 无状态认证 + 密码加盐加密实战
java·开发语言·spring boot·后端·安全·spring
期待のcode10 分钟前
MyBatis-Plus通用枚举
java·数据库·后端·mybatis·springboot