Android ListView 中子控件无法获取焦点的解决方法(主要针对键盘操作)

本文介绍如何通过设置ListView的android:descendantFocusability属性来避免子控件抢夺焦点,确保ListView的点击事件正常工作。同时提供了调整子控件setFocusable属性的方法,并实现了相应的Click函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

 
1。android:descendantFocusability
   一般情况下为了使用ListView的onItemClick,onItemLongClick方法,屏蔽子控件对焦点的获取,所以应将该值由
blocksDescendants 改为 afterDescendants
 
ps:
android:descendantFocusability
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.
Must be one of the following constant values.
Constant Value Description
beforeDescendants   0 The ViewGroup will get focus before any of its descendants.
afterDescendants   1 The ViewGroup will get focus only if none of its descendants want it.
blocksDescendants   2 The ViewGroup will block its descendants from receiving focus
 
2.在×××Adapter类的getView中调用子控件的setFocusable属性,及实现相应的Click函数(由于子控件获取焦点后,listview的onItemClick,onItemLongClick不再会被调用,
所以也一并实现,对于onItemLongClick函数,注意要返回TRUE,否则会发现该函数相应的代码会走两遍,估计是ListView的onItemLongClick被触发,有待深究)。
 
3.调用ListView的setItemsCanFocus(TRUE)函数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值