[英]Check box not getting checked after clicking listview
我有一個活動,其中有一個列表視圖,其中包含文本視圖和右側的復選框。 我希望在單擊列表視圖項時檢查復選框。 (不在復選框上)。 就像android用來逐個檢查消息一樣被刪除的那個。
任何人都可以幫我解決這個問題嗎? 我關掉了
android:focusable="false
和
android:focusableInTouchMode="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);
}
});
將android:clickable="false"
到CheckBox。
在您的情況下,最好使用CheckedTextView,並使用ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)
啟用多選。
AbsoluteSizeSpan/RelativeSizeSpan + SpannableStringBuilder
可以幫助您在一個TextView中實現不同的文本大小。
我的答案為時已晚,但效果很好。
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);
}
});
如果你想從BaseAdapter獲取變量值以獲取檢查位置或值等,請檢查此項
下面是我修改為添加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>
下面是在listAdapter上的getView中編寫的onClick方法
CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01);
chkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
((CheckedTextView) v).toggle();
}
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.