2012-11-22 26 views
0

我有一個活動,其中有一個列表視圖,其中包含文本視圖和右側的複選框。我希望複選框在點擊列表視圖項目時被選中。 (不在複選框上)。就像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); 
    } 
}); 
+4

請張貼一些代碼(XML佈局)。 – svenkapudija

+0

請發佈/分享代碼(xml的列表項)! – arjoan

+0

考慮使用[CheckedTextView](http://developer.android.com/reference/android/widget/CheckedTextView.html)(如'android.R.layout.simple_list_item_checked')或編寫自定義的可檢查佈局。 – Sam

回答

0

下面是我修改,添加CheckedtextView

<?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> 

下面是寫裏面getView上listAdapter

OnClick方法的XML
CheckedTextView chkBox = (CheckedTextView) findViewById(R.id.CheckedTextView01); 
     chkBox.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) 
      { 
       ((CheckedTextView) v).toggle(); 
      } 
     }); 
1

廣告d android:clickable="false"複選框。

在您的情況下,最好使用CheckedTextView,並使用ListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE)啓用多選。

AbsoluteSizeSpan/RelativeSizeSpan + SpannableStringBuilder可以幫助您在一個TextView中實現不同的文本大小。

1

我的回答爲時已晚,但效果很好。

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變量值越來越檢查位置或值等檢查this