RadioGroup中允许选择多个单选按钮

RadioGroup中允许选择多个单选按钮

本文介绍了RadioGroup中允许选择多个单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在XML中有两个单选按钮定义一个RadioGroup中。然而,我需要有显示按钮本身的左侧的标签,使标签左对齐和按钮右对齐。为了做到这一点,我使用含有一个TextView和单选一个RelativeLayout的,没有文本。这大约是什么样的布局是这样的:

I have a RadioGroup defined in XML that has two RadioButtons. However, I need to have the label displayed to the left of the button itself, with the label aligned left and the button aligned right. To do that, I used a RelativeLayout containing a TextView and a RadioButton with no text. This is approximately what the layout looks like:

<RadioGroup android:id="@+id/foo" android:layout_height="wrap_content" android:layout_width="match_parent">
    <RelativeLayout android:layout_height="wrap_content" android:layout_width="match_parent">
        <TextView android:text="@string/bar_one" android:layout_alignParentLeft="true" android:layout_height="wrap_content" android:layout_width="wrap_content" />
        <RadioButton android:id="@+id/bar1" android:layout_alignParentRight="true" android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </RelativeLayout>
    ...
</RadioGroup>

这显示我所期望的方式,但我所发现的是,RadioGroup中允许多于一个单选按钮被选中。当我删除RelativeLayouts和TextViews,所以单选按钮嵌套的直接在RadioGroup中,只有一个可以选择的。有没有一种方法来包装我的单选按钮,但保持一个以上的被选择在同一时间?

That displays the way I expect but what I've found is that the RadioGroup allows more than one RadioButton to be selected. When I remove the RelativeLayouts and TextViews, so the RadioButtons are nested directly under the RadioGroup, only one can be selected. Is there a way to wrap my RadioButtons but keep more than one from being selected at the same time?

如果不是,是否有更好的方法来完成造型我后?

If not, is there a better way to accomplish the styling I'm after?

推荐答案

这会更容易地告诉你完整的XML,因为你只显示一个单选所以没有办法知道的另一种性质是或截屏将是有益的。但是你可以使用的LinearLayout 和做一些像

It would be easier to tell with your full xml since you only show one RadioButton so there's no way to know what the properties of the other one is or a screen shot would be helpful. But you could use a LinearLayout and do something like

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...>
    <RadioGroup
     ...>
     <TextView
      .../>
     <RadioButton
     .../>
    <TextView
      .../>
    <RadioButton
    .../>
</RadioGroup>
</LinearLayout

但重读问题后,我想你可能要像

But after reading the question again I think you might want something like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <RadioGroup
        android:id="@+id/foo"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        android:layout_alignParentRight="true" >

        <RadioButton android:id="@+id/bar1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <RadioButton
            android:id="@+id/bar2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>

    <TextView android:id="@+id/textView1"
        android:text="Text 1"
        android:layout_alignParentLeft="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_alignBottom="@+id/bar1"/>

    <TextView android:text="Text 2"
        android:layout_alignParentLeft="true"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_alignBottom="@+id/foo" />
</RelativeLayout>

您可能需要调整一些填充和/或保证金或进行其他调整,只是得到它如何想,但我认为这将让你接近。让我知道如果我误解了

You may have to adjust some padding and/or margin or make other adjustments to get it just how you want it but I think this will get you close. Let me know if I misunderstood

这篇关于RadioGroup中允许选择多个单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 14:26