Android 使用复选框创建ListView

Android 使用复选框创建ListView,android,listview,Android,Listview,我有一个列表视图,每个项目都包含一个图像、一个文本视图和一个复选框。我试图让它,所以无论你在项目内点击它突出显示复选框。正如响应中所述,我将复选框的focusable属性设置为false,但我仍然只能单击复选框来更改其状态。更有趣的是,当我点击一个复选框时,每一项都会突出显示。有什么建议吗 这是我的一个项目的xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_

我有一个列表视图,每个项目都包含一个图像、一个文本视图和一个复选框。我试图让它,所以无论你在项目内点击它突出显示复选框。正如响应中所述,我将复选框的focusable属性设置为false,但我仍然只能单击复选框来更改其状态。更有趣的是,当我点击一个复选框时,每一项都会突出显示。有什么建议吗

这是我的一个项目的xml

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/image"
        android:layout_width="50dip"
        android:layout_height="50dip" 
        android:src="@drawable/stub"   
        android:scaleType="centerCrop"/>
    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" 
        android:layout_gravity="left|center_vertical" 
        android:textSize="20dip" 
        android:layout_marginLeft="10dip"/>

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"/>

</LinearLayout>
主xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"/>
<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:text="Clear Cache"/>
</LinearLayout>
在main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:layout_marginTop="5dp">

    <RelativeLayout
        android:layout_width="450dp" 
        android:layout_height="25dp"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="3dp">

     <ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" 
android:layout_weight="1"/>
     </RelativeLayout>



     <RelativeLayout
        android:layout_width="450dp" 
        android:layout_height="25dp"
        android:layout_marginLeft="7dp"
        android:layout_marginTop="3dp">

     <Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
android:text="Clear Cache"/>
     </RelativeLayout>

</LinearLayout>

希望这对您有所帮助…

在填充列表的过程中,您可以设置一个侦听器并将复选框更改为选中。试着这样做:

list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                ListItem item = (ListItem) adapter.getItem(position);
                if(item.checkbox.isChecked()) 
                      item.checkbox.setChecked(false);
                else
                      item.checkbox.setChecked(true);
            }
 });
为ListView的项目布局容器创建CheckableLinearLayout或CheckableRelativeLayout, 将listview的模式设置为“MultipleChice” 请在以下链接中查找更多详细信息:


这一个看起来与你的类似:我对你输入的方式有点困惑,我能得到一点澄清吗?我想也是同样的问题,如果你想把你的a按钮放在你的主要listview xml上,像这样做检查我的更新答案,并为你的复选框维护我的其他类型的xml,当然,把你的尺寸值放在RealtivesLayouts上。也试过了,结果没有改变。