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
相关推荐
Flywith2438 分钟前
【每日一技】Raycast 实现 scrcpy 的快捷显示隐藏
android·前端
没有了遇见2 小时前
Android(Coil,Glide)大量图片加载缓存清理问题(二 Coil处理)
android
Assby2 小时前
从洋葱模型看Java与Go的设计哲学:为什么它们如此不同?
java·后端·架构
城东米粉儿2 小时前
Android Dagger2笔记
android
没有了遇见2 小时前
Android(Coil,Glide)大量图片加载缓存清理问题(一)
android
恋猫de小郭2 小时前
谷歌 Genkit Dart 正式发布:现在可以使用 Dart 和 Flutter 构建全栈 AI 应用
android·前端·flutter
belhomme3 小时前
(面试题)Netty 线程模型
java·面试·netty
曾经我也有梦想4 小时前
Day4 Kotlin 高级特性
android
simplepeng4 小时前
Compose Multiplatform 中的 Navigation 3
android