您的位置:首页 > 移动开发 > Android开发

android ListView Item和CheckBox、Button点击事件冲突的问题

2015-12-18 00:00 766 查看
摘要: 不让布局文件中的CheckBox和Button获取焦点的方法

在Item的根目录上设置属性:

//表示锁定子View的焦点,不让Layout下的子View获取
android:descendantFocusability="blocksDescendants"


CheckBox和Button设置属性:

//三个属性都需要设置为false
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

完整的布局文件示例:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:descendantFocusability="blocksDescendants"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="horizontal">
<CheckBox
android:id="@+id/box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:visibility="gone"
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"
android:layout_marginLeft="8dp"
android:button="@drawable/check_box_background"
android:gravity="center_vertical" />

<TextView
android:id="@+id/text_name"
android:layout_width="wrap_content"
android:paddingRight="5dp"
android:layout_weight="0.1"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginLeft="20dp"
android:text="点名"
android:textColor="@color/gray2"
android:textSize="14sp" />

<TextView
android:id="@+id/text_workinfo"
android:layout_width="0dp"
android:layout_weight="0.99"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:minHeight="45dp"
android:layout_marginRight="5dp"
android:text="0.0"
android:textColor="@color/green"
android:textSize="14sp" />

<TextView
android:id="@+id/text_edit"
android:layout_width="0dp"
android:layout_weight="0.4"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginRight="5dp"
android:text="编辑"
android:textColor="@color/black"
android:background="@drawable/border_line_corner_blue"
android:textSize="14sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:visibility="visible"
android:background="@color/gray" />
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息