之前在项目中遇到了一个问题,就是一个ListView,我在Item中设置了Button,类似下图的样子(这里只是个例子)



“编辑”和“删除”都能点击,但是当我设置了onItemClickListener后,点击Item却没有了反应。

查了一些资料,最后了解到,在Item的xml文件中最外层的View上加上如下属性:

android:descendantFocusability="blocksDescendants"

onItemClickListener就能使用了。看来这是一个获取焦点的问题,Item失效的原因就是无法获取焦点。


上了Android官网查看,发现该属性在ViewGroup类中就有了


点进去看详细信息:

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.

This corresponds to the global attribute resource symbol descendantFocusability.

before就是ViewGroup会先于所有子View获取焦点

after就是当所有子View都不要焦点时ViewGroup才会得到焦点

blocks就是ViewGroup会阻碍它的子View获取焦点


三种方法,大家可以选择适合自己代码口味的.......



更多相关文章

  1. android如何调用其他应用的方法或属性
  2. Android(安卓)Edittext显示输入法 搜索 发送 完成 下一项 上一项
  3. Android(安卓)动画机制(二)
  4. android shape的使用详解
  5. Android(安卓)pppd_gprs脚本启动过程
  6. 使得EditText失去焦点
  7. 【android学习笔记】关于相对布局RelativeLayout的各种属性介绍
  8. Android(安卓)Drawable 系列——ClipDrawable
  9. Android(安卓)属性设置android:noHistory="true"

随机推荐

  1. android Volley 使用
  2. 【Android】Inconvertible types:cannot
  3. androidUI控件下载地址
  4. Android TXT文件读写
  5. 常用方法(1)------根据图片的url路径获得
  6. Android获取View位置
  7. Android: android.content.res.Resources
  8. 申请Android(安卓)Google Map API key
  9. Android 服务器连接工具类HttpUtil
  10. Android(安卓)设置亮度