2012-03-26 217 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