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
相关推荐
MediaTea5 分钟前
Python:实例 __dict__ 详解
java·linux·前端·数据库·python
2501_915918418 分钟前
iOS 开发中证书创建与管理中的常见问题
android·ios·小程序·https·uni-app·iphone·webview
个案命题22 分钟前
鸿蒙ArkUI组件通信专家:@Param装饰器的奇幻漂流
java·服务器·前端
CodeCraft Studio29 分钟前
Excel处理控件Aspose.Cells教程:使用C#在Excel中创建折线图
java·c#·excel·aspose.cells·excel图表·excel api库·excel折线图
00后程序员张31 分钟前
IOScer 开发环境证书包括哪些,证书、描述文件与 App ID 的协同管理实践
android·ios·小程序·https·uni-app·iphone·webview
子超兄38 分钟前
Bean生命周期
java·spring
程序员阿鹏40 分钟前
事务与 ACID 及失效场景
java·开发语言·数据库
程序员清风41 分钟前
阿里二面:新生代垃圾回收为啥使用标记复制算法?
java·后端·面试
sino爱学习42 分钟前
Java 三元表达式(?:)的常见坑总结
java·后端
❀͜͡傀儡师1 小时前
Spring Boot函数式编程:轻量级路由函数替代传统Controller
java·spring boot·后端