2012-03-26 176 views
0

我有一個ListView單選按鈕選擇。

<ListView 
     android:id="@+id/my_list" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

ListView的每一行的佈局是:

<? Xml version="1.0"encoding="utf-8"?> 
<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <ImageView 
      android:id="@+id/my_img" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_algnParentLeft="true" 
      android:layout_centerVertical="true"   
      /> 
    <TextView 
     android:id="@+id/my_value" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/my_img" 
     android:textColor="@drawable/black" /> 
    <RadioButton 
     android:id="@+id/my_radio" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_toRightOf="@id/my_value" /> 

</ RelativeLayout> 

正如你看到的上面,對列表中的每一行,如何實現RadioButton以下兩個特點視爲RadioButton

1.假設列表中有3項,如何使每行上的單選按鈕在列表中執行單選? (默認情況下,我可以選擇所有單選按鈕)

2.如何擁有對單選按鈕選擇每個項目行區域點擊,而不是隻點擊單選按鈕進行選擇?

+0

也許幫助http://stackoverflow.com/questions/7329856/how-to-use-radiogroup-in-listview-custom-adapter – 2012-03-26 11:39:23

回答

0

獲取到你的ListView參考,設置其屬性爲

lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); 

而且從你的XML佈局中刪除的單選按鈕。上述方法會自動在每個列表視圖行中放置一個單選按鈕。 用途:

<? Xml version = "1.0" encoding = "utf-8"?> 
<RelativeLayout 
    xmlns: android = "http://schemas.android.com/apk/res/android" 
    android: layout_width = "fill_parent" 
    android: layout_height = "fill_parent" 
    > 
    <ImageView 
      android: id = "@+id/my_img" 
      android: layout_width = "wrap_content" 
      android: layout_height = "wrap_content" 
      android:layout_algnParentLeft="true" 
      android: layout_centerVertical = "true"   
      /> 
    <TextView 
     android: id = "@+id/my_value" 
     android: layout_width = "wrap_content" 
     android: layout_height = "wrap_content" 
     android:layout_toRightOf="@id/my_img" 
     android: textColor = "@drawable/ black" /> 


</ RelativeLayout> 
+0

我想就行區域時使用的點擊(無論它是單選按鈕還是圖像圖標),單選按鈕都會被檢查,該怎麼做? – 2012-03-26 12:14:21

+0

將選擇模式設置爲單一後,單擊行時,該行的單選按鈕將被選中,其他選項將被選中。 – Akhil 2012-03-26 12:52:29

+0

我從我的行佈局中刪除,並將單選模式添加到我的列表中,但沒有單選按鈕。你可以更清楚,單選按鈕的定義在哪裏? – 2012-03-26 13:19:11