2011-03-28 102 views
2

我的问题与this question类似,因为我无法找到一个令人满意的答案,我想明确提出我的问题。如何在自定义适配器中组单选按钮?

我有一个自定义的适配器,它需要一个字符串数组,并且必须使用该数组填充列表(在我的ListActivity类中定义)。现在,每行的布局有一个文本视图和一个单选按钮,如图

layout.xml

<?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="wrap_content"> 
<TextView android:text="@+id/word" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/word" 
    android:textSize="30px"> 
</TextView> 
<RadioButton 
    android:id="@+id/selection" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="4px" 
    android:layout_marginRight="10px" 
    android:layout_alignParentRight="true" 
    android:layout_x="27px" 
    android:layout_y="8px"> 
</RadioButton> 

一切,直到这点工作的罚款。我能够用单选按钮呈现每一行。 我在寻找的是一种可以将这些单选按钮组合在一起执行单一选择的方法。 这是我正在制作的应用程序所需的,其中有四个选项作为答案。 也请告诉我这是否是这种问题的正确方法。 在此先感谢

+0

看看我编辑的答案 – apesa 2011-04-01 00:45:53

回答

0

我本来可以更清楚。无论如何,你的最终组装的XML需要看起来像这样。你可以在代码中这样做,只要你正在构建的东西被返回,那就没问题了。每个RadioButton必须属于RadioGroup容器。

<RadioGroup 
      android:id="@+id/rg_configup1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_centerVertical="true" 
      android:layout_centerHorizontal="true" 
      > 
      <RadioButton 
       android:id="@+id/rb_configup1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Sales" 
       android:layout_centerVertical="true" 
       android:layout_centerHorizontal="true" 
       > 
      </RadioButton> 
      <RadioButton 
       android:id="@+id/rb_configup2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Regional" 
       android:layout_above="@+id/rb_configup1" 
       android:layout_alignLeft="@+id/rb_configup1" 
       > 
      </RadioButton> 
      <RadioButton 
       android:id="@+id/rb_configup3" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Global" 
       android:layout_above="@+id/rb_configup2" 
       android:layout_alignLeft="@+id/rb_configup1" 
       > 
      </RadioButton> 
      <RadioButton 
       android:id="@+id/rb_configup4" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Ad-hoc" 
       android:layout_above="@+id/rb_configup3" 
       android:layout_alignLeft="@+id/rb_configup1" 
       > 
      </RadioButton> 
     </RadioGroup> 

这是否有意义,或者您是否正在尝试在XML以外的其他位置执行此操作?

+0

ihad早些时候尝试过。这不会将单选按钮分组。我通过在我的自定义适配器中使用(RadioButton)view.findViewById(R.id.selection)来使用单选按钮。使用上面的代码不会分组我的按钮。我仍然可以将它们全部选中 – ric03uec 2011-03-28 20:42:20

+0

这是我用来在任何时候只允许选择四个单选按钮中的一个。这个对我有用。将屏幕上的单选按钮分组为单选按钮我正在使用相关布局 – apesa 2011-03-28 20:43:52

+0

我正在使用与eventlistener相同的处理,并且在屏幕上有四个单选按钮。 – apesa 2011-03-28 20:46:36