单选按钮选择。

时间:2012-03-26 11:33:34

标签: android android-layout android-widget

我有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. 如何为单选按钮选择单击每个项目行区域而不是仅单击单选按钮进行选择?

1 个答案:

答案 0 :(得分: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>