Android系统.ListView中的RadioButtons

Tim*_*ima 6 android listview

我有动态生成的ListView,它包含单选按钮作为列表项.

是否可以在该列表视图中使用无线电组功能或这些无线电按钮.

我的意思是,我想,如果用户选择一个单选按钮,之前选择的单选按钮将被取消选择.

这是我目前的解决方案,我不太喜欢.我只保存选中的单选按钮,如果选择了另一个单选按钮,则取消选择保存的单选按钮.

感谢您的建议或链接.

这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:background="@drawable/bg_tile"
 android:padding="10dp">
 <TextView
  android:id="@+id/text_station_name"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:padding="5dp"
  android:text="TEST"
  android:textColor="@color/black"
  android:background="@color/transparent_white"/>
 <ListView
  android:id="@+id/list_lines"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"/>    
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RadioButton xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/rb_lineId"    
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:textColor="@color/black"
 android:paddingLeft="50dp"
 android:background="@drawable/selector_custombutton"/>  
Run Code Online (Sandbox Code Playgroud)

但我也试过这个:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:orientation="vertical"
 android:background="@drawable/bg_tile"
 android:padding="10dp">
 <TextView
  android:id="@+id/text_station_name"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:padding="5dp"
  android:text="TEST"
  android:textColor="@color/black"
  android:background="@color/transparent_white"/>
    <RadioGroup
        android:layout_width="fill_parent"
       android:layout_height="wrap_content"
        android:orientation="vertical">  
  <ListView
   android:id="@+id/list_lines"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"/>
 </RadioGroup>    
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

Fel*_*lix 2

我想你想要android:choiceMode="singleChoice"。实施起来有点棘手(我自己也不知道具体细节),但这是一个起点。

  • 感谢您的起点,但不知道从哪里开始?! (2认同)
  • 我通常只是[搜索 Google 代码搜索](http://google.com/codesearch?q=android:choiceMode%3D%22singleChoice%22) 来查找使用特定功能的其他项目,然后看看如何使用。继续,点击它:) (2认同)