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
相关推荐
草履虫建模3 小时前
力扣算法 1768. 交替合并字符串
java·开发语言·算法·leetcode·职场和发展·idea·基础
Rainman博5 小时前
WMS-窗口relayout&FinishDrawing
android
qq_297574676 小时前
【实战教程】SpringBoot 实现多文件批量下载并打包为 ZIP 压缩包
java·spring boot·后端
老毛肚6 小时前
MyBatis插件原理及Spring集成
java·spring·mybatis
学嵌入式的小杨同学6 小时前
【Linux 封神之路】信号编程全解析:从信号基础到 MP3 播放器实战(含核心 API 与避坑指南)
java·linux·c语言·开发语言·vscode·vim·ux
lang201509286 小时前
JSR-340 :高性能Web开发新标准
java·前端·servlet
Re.不晚6 小时前
Java入门17——异常
java·开发语言
缘空如是6 小时前
基础工具包之JSON 工厂类
java·json·json切换
追逐梦想的张小年7 小时前
JUC编程04
java·idea
好家伙VCC7 小时前
### WebRTC技术:实时通信的革新与实现####webRTC(Web Real-TimeComm
java·前端·python·webrtc