Android 单击listview后未选中复选框

Android 单击listview后未选中复选框,android,android-listview,android-checkbox,Android,Android Listview,Android Checkbox,我有一个活动,其中有一个列表视图,有文本视图,右侧有一个复选框。我希望在单击listview项目时选中复选框。(不在复选框上)。就像android用来逐个检查要删除的消息一样 有人能帮我解决这个问题吗?我关掉了 android:focusable="false 及 打开复选框 下面是我的列表视图项xml <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android

我有一个活动,其中有一个列表视图,有文本视图,右侧有一个复选框。我希望在单击listview项目时选中复选框。(不在复选框上)。就像android用来逐个检查要删除的消息一样

有人能帮我解决这个问题吗?我关掉了

android:focusable="false

打开复选框

下面是我的列表视图项xml

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="1."
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@android:color/white" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="18dp"
    android:layout_toRightOf="@+id/textView2"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/textView1"
    android:layout_marginRight="22dp"
    android:layout_marginTop="-15dp" 
    android:focusable="false"
    android:focusableInTouchMode="false"/>

这是我的代码:

list.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
        // TODO Auto-generated method stub
        if(checkbox.isChecked())
            checkbox.setChecked(false);

        else
            checkbox.setChecked(true);
    }
});
list.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
//TODO自动生成的方法存根
如果(checkbox.isChecked())
checkbox.setChecked(false);
其他的
checkbox.setChecked(true);
}
});

下面是我修改以添加CheckedtextView的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


        <CheckedTextView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/checkedTextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checkMark="?android:attr/listChoiceIndicatorMultiple"
            android:gravity="center_vertical"
            android:paddingLeft="6dip"
            android:paddingRight="6dip"
            android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

将android:clickable=“false”添加到复选框中

在您的情况下,最好使用CheckedTextView,并使用
ListView.setChoiceMode(ListView.CHOICE\u MODE\u MULTIPLE)
启用多选


绝对大小span/RelativeSizeSpan+SpannableStringBuilder
可以帮助您在一个文本视图中实现不同的文本大小。

我的答案太晚了,但效果非常好

list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
            CheckBox cb = (CheckBox) arg1.findViewById(R.id.checkBox1);
              TextView tv = (TextView) arg1.findViewById(R.id.textView1);
              cb.performClick(); //this will trigger the checkbox
              //do here 
              if(checkbox.isChecked())
                  checkbox.setChecked(false);
              else
                  checkbox.setChecked(true);
        }
});
list.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共视图单击(AdapterView arg0、视图arg1、整型arg2、长型arg3){
复选框cb=(复选框)arg1.findViewById(R.id.checkBox1);
TextView tv=(TextView)arg1.findViewById(R.id.textView1);
cb.performClick();//这将触发复选框
//在这里做什么
如果(checkbox.isChecked())
checkbox.setChecked(false);
其他的
checkbox.setChecked(true);
}
});

如果你想从BaseAdapter获取变量值,以获取检查的位置或值等检查

请发布一些代码(XML布局)。请发布/共享代码(列表项的XML)!考虑使用一个(例如,代码> Android .r.Dealth.SimeLyListItMyEngult:<代码>)或编写一个自定义的可检查布局。将这个属性指定给你的主要RelealayOutoDeal:DealEntAdvActudio =“BaskS后代”,你的ListVIEW在哪里?
CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
        chkBox.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v)
            {
                ((CheckedTextView) v).toggle();
            }
        });
list.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
            CheckBox cb = (CheckBox) arg1.findViewById(R.id.checkBox1);
              TextView tv = (TextView) arg1.findViewById(R.id.textView1);
              cb.performClick(); //this will trigger the checkbox
              //do here 
              if(checkbox.isChecked())
                  checkbox.setChecked(false);
              else
                  checkbox.setChecked(true);
        }
});