3

我正在尝试创建一个 2x2 单选按钮数组,但我不知道如何自定义除水平或垂直之外的布局。有任何想法吗?

我只得到

A B C D

A
B
C
D

但我想拥有

A B
C D

编辑:我解决了这个问题。对于任何想知道的人,我建立了两个独立的广播组(即一个与 AB 和一个与 CD)。我为每个 RadioButton 设置了 onClickListener(),并在单击第一个 RadioGroup 中的按钮时在第二个 RadioGroup 上使用了 clearCheck(),反之亦然。

4

3 回答 3

3

你可以很容易地通过LinearLayout在你的RadioGroupwith中包含两个来做到这一点orientation="horizontal"

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

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="true"
                    android:text="18-24"
                    />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="true"
                    android:text="36-45"
                    />
            </LinearLayout>

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="true"
                    android:text="25-35"
                    />

                <RadioButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textAllCaps="true"
                    android:text=">45"
                    />
            </LinearLayout>

于 2015-06-25T09:30:18.120 回答
2

上述示例的更好解决方案。当我们必须使用没有单选组的单选按钮时,应该使用它。

    ///////////////////////////  for call setting rb ////////////////////////////////////////////////////

    grbtn = (RadioButton) findViewById(R.id.grbtn);
    vrbtn = (RadioButton) findViewById(R.id.vrbtn);
    srbtn = (RadioButton) findViewById(R.id.srbtn);
    arbtn = (RadioButton) findViewById(R.id.arbtn);

    grbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            vrbtn.setChecked(false);
            srbtn.setChecked(false);
            arbtn.setChecked(false);
            ///////////////////////////////////////////////

        }
    });

    vrbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            grbtn.setChecked(false);
            srbtn.setChecked(false);
            arbtn.setChecked(false);
            ///////////////////////////////////////////////

        }
    });

    srbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            vrbtn.setChecked(false);
            grbtn.setChecked(false);
            arbtn.setChecked(false);
            ///////////////////////////////////////////////

        }
    });

    arbtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            vrbtn.setChecked(false);
            srbtn.setChecked(false);
            grbtn.setChecked(false);
            ///////////////////////////////////////////////

        }
    });

    /////////////////////////////////////////////////////////////////////////////////////////////////////


`
于 2015-03-24T07:09:31.343 回答
0

如果这是您要问的,这就是我会做的。可能有更好的方法,但这对我来说已经足够好了。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/radioGroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_gravity="top|left">
    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioOneText" />
    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioTwoText"
        android:layout_toRightOf="@id/radio1"
        android:layout_alignBaseline="@id/radio1"
        android:layout_marginLeft="10dp" />
    <RadioButton
        android:id="@+id/radio3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioThreeText"
        android:layout_below="@id/radio1"
        android:layout_alignLeft="@id/radio1" />
    <RadioButton
        android:id="@+id/radio4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radioFourText"
        android:layout_below="@id/radio2"
        android:layout_toRightOf="@id/radio3"
        android:layout_alignBaseline="@id/radio3"
        android:layout_alignLeft="@id/radio2" />
</RelativeLayout>

这是使用“setOnClickListener”处理更改的示例...

package com.example.breakable;

import android.os.*;
import android.app.*;
import android.view.View;
import android.widget.*;

public class MainActivity extends Activity
{   
    RadioButton radioOne;
    RadioButton radioTwo;
    RadioButton radioThree;
    RadioButton radioFour;
    @Override
    public void onCreate(Bundle icicle)
    {       
        super.onCreate(icicle);
        setContentView(R.layout.main);

        radioOne = (RadioButton)findViewById(R.id.radio1);
        radioTwo = (RadioButton)findViewById(R.id.radio2);
        radioThree = (RadioButton)findViewById(R.id.radio3);
        radioFour = (RadioButton)findViewById(R.id.radio4);

        radioOne.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (radioTwo.isChecked()){
                    radioTwo.setChecked(false);
                    radioOne.setChecked(true);
                } else if (radioThree.isChecked()) {
                    radioThree.setChecked(false);
                    radioOne.setChecked(true);
                } else if (radioFour.isChecked()) {
                    radioFour.setChecked(false);
                    radioOne.setChecked(true);
                } else {
                    radioOne.setChecked(true);
                }
            }
        });
        radioTwo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioOne.isChecked()){
                    radioOne.setChecked(false);
                    radioTwo.setChecked(true);
                } else if (radioThree.isChecked()) {
                    radioThree.setChecked(false);
                    radioTwo.setChecked(true);
                } else if (radioFour.isChecked()) {
                    radioFour.setChecked(false);
                    radioTwo.setChecked(true);
                } else {
                    radioTwo.setChecked(true);
                }
            }
        });
        radioThree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioOne.isChecked()){
                    radioOne.setChecked(false);
                    radioThree.setChecked(true);
                } else if (radioTwo.isChecked()) {
                    radioTwo.setChecked(false);
                    radioThree.setChecked(true);
                } else if (radioFour.isChecked()) {
                    radioFour.setChecked(false);
                    radioThree.setChecked(true);
                } else {
                    radioThree.setChecked(true);
                }
            }
        });
        radioFour.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (radioOne.isChecked()){
                    radioOne.setChecked(false);
                    radioFour.setChecked(true);
                } else if (radioTwo.isChecked()) {
                    radioTwo.setChecked(false);
                    radioFour.setChecked(true);
                } else if (radioThree.isChecked()) {
                    radioThree.setChecked(false);
                    radioFour.setChecked(true);
                } else {
                    radioFour.setChecked(true);
                }
            }
        });
    }
}
于 2014-06-13T14:32:44.940 回答