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
相关推荐
韩师学子--小倪22 分钟前
fastjson与gson的toString差异
java·json
Drawing stars24 分钟前
JAVA后端 前端 大模型应用 学习路线
java·前端·学习
nbsaas-boot36 分钟前
SQL Server 存储过程开发规范(公司内部模板)
java·服务器·数据库
行百里er1 小时前
用 ThreadLocal + Deque 打造一个“线程专属的调用栈” —— Spring Insight 的上下文管理术
java·后端·架构
玄〤1 小时前
黑马点评中 VoucherOrderServiceImpl 实现类中的一人一单实现解析(单机部署)
java·数据库·redis·笔记·后端·mybatis·springboot
J_liaty1 小时前
Spring Boot拦截器与过滤器深度解析
java·spring boot·后端·interceptor·filter
云诗卡达2 小时前
Flutter安卓APP接入极光推送和本地通知
android·flutter
亲爱的非洲野猪2 小时前
Java锁机制八股文
java·开发语言
rgeshfgreh2 小时前
C++字符串处理:STL string终极指南
java·jvm·算法
Zoey的笔记本2 小时前
「支持ISO27001的GTD协作平台」数据生命周期管理方案与加密通信协议
java·前端·数据库