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
相关推荐
i***279512 小时前
springboot集成onlyoffice(部署+开发)
java·spring boot·后端
safestar201213 小时前
Elasticsearch ILM实战:从数据热恋到冷静归档的自动化管理
java·开发语言·jvm·elasticsearch·es
霸王大陆13 小时前
《零基础学 PHP:从入门到实战》教程-模块四:数组与函数-2
android·开发语言·php
i***t91913 小时前
SQL 实战:复杂数据去重与唯一值提取
java
o***111413 小时前
SpringBoot线程池的使用
java·spring boot·后端
6***v41713 小时前
SpringBoot下获取resources目录下文件的常用方法
java·spring boot·后端
v***431713 小时前
springboot3整合knife4j详细版,包会!(不带swagger2玩)
android·前端·后端
程序员西西13 小时前
Spring Cloud实战总结:90%开发者踩过的坑,这篇一次性帮你填平!
java·后端
t***821113 小时前
【Spring】Cookie和Session是什么
java·后端·spring
y***136413 小时前
springboot和springframework版本依赖关系
java·spring boot·后端