如何将2x2单选按钮分配到android中的组中

时间:2016-04-22 16:39:44

标签: android xml

我是Android的新手请让我知道我做错了什么,我添加了一个无线电组,然后添加了Gridlayout以2x2形式的矩阵对齐单选按钮,它对齐好,但问题是它不是选择任何单选按钮时取消选择以前选择的选项。我怎样才能做到这一点?

All radio buttons are getting selected. None of them are deselecting upon changing selection of radio button. Please suggest me any approach.

Thanks

Please check attached image for reference

<RadioGroup
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New RadioButton"
                android:id="@+id/radOption1"
                android:layout_row="0"
                android:layout_column="0"
                android:padding="5dp" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New RadioButton"
                android:id="@+id/radOption2"
                android:layout_row="0"
                android:layout_column="1"
                android:padding="5dp" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New RadioButton"
                android:id="@+id/radOption3"
                android:layout_row="1"
                android:layout_column="0"
                android:padding="5dp" />

            <RadioButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="New RadioButton"
                android:id="@+id/radOption4"
                android:layout_row="1"
                android:layout_column="1"
                android:padding="5dp" />

    </GridLayout>
</RadioGroup>

1 个答案:

答案 0 :(得分:1)

RadioGroup基本上是LinearLayout,您可以在RadioButtons内调整layout。它不像通用GridLayout那样可以存储任何内容(在您的示例中为RadioButtons),它将处理您将在层次结构中放置的任何RadioGroup

TLDR:RadioButtons可让您水平或垂直对齐LinearLayout - var x; function filterName(n) { x = document.getElementsByClassName(n); console.log(x.length); for (i = 0; i < x.length; i++) { x[i].setAttribute("class", "hidden"); } }

您需要做的事情在此question中进行了描述。