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
相关推荐
萧曵 丶5 分钟前
JDK各版本新增特性详解
java·面试
TeleostNaCl6 分钟前
Android | 启用 TextView 跑马灯效果的方法
android·经验分享·android runtime
毅炼29 分钟前
hot100打卡——day08
java·数据结构·算法·leetcode·深度优先
a努力。38 分钟前
国家电网Java面试被问:慢查询的优化方案
java·开发语言·面试
@小码农1 小时前
202512 电子学会 Scratch图形化编程等级考试四级真题(附答案)
java·开发语言·算法
TheNextByte11 小时前
Android USB文件传输无法使用?5种解决方法
android
程序猿ZhangSir1 小时前
深入理解 BIO,NIO,AIO 三者的用途和区别?Select,poll,epoll 操作系统函数简介
java·spring·nio
智航GIS1 小时前
6.2 while循环
java·前端·python
2201_757830871 小时前
AOP核心概念
java·前端·数据库
为所欲为、Lynn1 小时前
用FastJson的Filter自动映射枚举
java·spring boot