如何在不同的无线电组上设置不同的单选按钮?

时间:2017-04-24 13:17:48

标签: android

我有3个放射性组,每组有2个无线电按钮。因为我需要它们在垂直和水平位置。

XML布局:

        <RadioGroup
            android:id="@+id/radiogroup_Mapeamento1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_movimentada"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="@color/cardview_dark_background"
                android:text="Rua Movimentada"
                android:textColor="@color/cardview_dark_background" />

            <RadioButton
                android:id="@+id/rb_iluminacao"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:buttonTint="@color/cardview_dark_background"
                android:text="Má Iluminação"
                android:textColor="@color/cardview_dark_background" />

        </RadioGroup>

        <RadioGroup
            android:id="@+id/radiogroup_Mapeamento2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_comercio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="@color/cardview_dark_background"
                android:text="Comércio Aberto"
                android:textColor="@color/cardview_dark_background" />

            <RadioButton
                android:id="@+id/rb_assedio"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="15dp"
                android:buttonTint="@color/cardview_dark_background"
                android:text="Assédio Recorrente"
                android:textColor="@color/cardview_dark_background" />

        </RadioGroup>

        <RadioGroup
            android:id="@+id/radiogroup_Mapeamento3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="10dp"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/rb_policia"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:buttonTint="@color/cardview_dark_background"
                android:text="Posto Policial"
                android:textColor="@color/cardview_dark_background" />

            <RadioButton
                android:id="@+id/rb_porteiro"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="35dp"
                android:buttonTint="@color/cardview_dark_background"
                android:text="Porteiro/Segurança"
                android:textColor="@color/cardview_dark_background" />

        </RadioGroup>

如何只设置1个radiobutton?或者,布局是否只有1个无线电组?

这样的事情:

        rg1 = (RadioGroup)findViewById(R.id.radiogroup_Map1);
        rg2 = (RadioGroup)findViewById(R.id.radiogroup_Map2);
        rg3 = (RadioGroup)findViewById(R.id.radiogroup_Map3);

rg1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                op1 = rg1.getCheckedRadioButtonId();
                switch (op1){
                    case R.id.rb_1:
                        rb_1.setChecked(true);
                        rb_2.setChecked(false);
                        rb_3.setChecked(false);
                        rb_4.setChecked(false);
                        rb_5.setChecked(false);
                        rb_6.setChecked(false);
                        break;
                    case R.id.rb_2:
                        rb_1.setChecked(false);
                        rb_2.setChecked(true);
                        rb_3.setChecked(false);
                        rb_4.setChecked(false);
                        rb_5.setChecked(false);
                        rb_6.setChecked(false);
                        break;
                }
            }
        });

rg2.setOnClick.....

1 个答案:

答案 0 :(得分:0)

您可以使用内部RadioGroup的单RadioGroup,而不是使用多个LinearLayout

以下是示例布局activity_radio_group.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_radio_group"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp">

    <RadioGroup
        android:id="@+id/radioGroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

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

                <RadioButton
                    android:id="@+id/button1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button 1"/>

                <RadioButton
                    android:id="@+id/button2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button 2"/>
            </LinearLayout>

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

                <RadioButton
                    android:id="@+id/button3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button 3"/>

                <RadioButton
                    android:id="@+id/button4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button 4"/>
            </LinearLayout>

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

                <RadioButton
                    android:id="@+id/button5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button 5"/>

                <RadioButton
                    android:id="@+id/button6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Button 6"/>
            </LinearLayout>

        </LinearLayout>
    </RadioGroup>
</RelativeLayout>

RadioButton改变programmatically个州Activity

public class RadioGroupActivity extends AppCompatActivity implements RadioButton.OnCheckedChangeListener {

    RadioGroup radioGroup;
    RadioButton radioButton1;
    RadioButton radioButton2;
    RadioButton radioButton3;
    RadioButton radioButton4;
    RadioButton radioButton5;
    RadioButton radioButton6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_group);

        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioButton1 = (RadioButton) findViewById(R.id.button1);
        radioButton2 = (RadioButton) findViewById(R.id.button2);
        radioButton3 = (RadioButton) findViewById(R.id.button3);
        radioButton4 = (RadioButton) findViewById(R.id.button4);
        radioButton5 = (RadioButton) findViewById(R.id.button5);
        radioButton6 = (RadioButton) findViewById(R.id.button6);

        radioButton1.setOnCheckedChangeListener(this);
        radioButton2.setOnCheckedChangeListener(this);
        radioButton3.setOnCheckedChangeListener(this);
        radioButton4.setOnCheckedChangeListener(this);
        radioButton5.setOnCheckedChangeListener(this);
        radioButton6.setOnCheckedChangeListener(this);

    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean status) {

        if (status)
        {
            switch (compoundButton.getId()) {
                case R.id.button1:
                    radioButton2.setChecked(false); radioButton3.setChecked(false); radioButton4.setChecked(false);
                    radioButton5.setChecked(false); radioButton6.setChecked(false);

                    // Do something
                    Toast.makeText(getApplicationContext(), radioButton1.getText() + " clicked", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.button2:
                    radioButton1.setChecked(false); radioButton3.setChecked(false); radioButton4.setChecked(false);
                    radioButton5.setChecked(false); radioButton6.setChecked(false);

                    // Do something
                    Toast.makeText(getApplicationContext(), radioButton2.getText() + " clicked", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.button3:
                    radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton4.setChecked(false);
                    radioButton5.setChecked(false); radioButton6.setChecked(false);

                    // Do something
                    Toast.makeText(getApplicationContext(), radioButton3.getText() + " clicked", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.button4:
                    radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton3.setChecked(false);
                    radioButton5.setChecked(false); radioButton6.setChecked(false);

                    // Do something
                    Toast.makeText(getApplicationContext(), radioButton4.getText() + " clicked", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.button5:
                    radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton3.setChecked(false);
                    radioButton4.setChecked(false); radioButton6.setChecked(false);

                    // Do something
                    Toast.makeText(getApplicationContext(), radioButton5.getText() + " clicked", Toast.LENGTH_SHORT).show();
                    break;

                case R.id.button6:
                    radioButton1.setChecked(false); radioButton2.setChecked(false); radioButton3.setChecked(false);
                    radioButton4.setChecked(false); radioButton5.setChecked(false);

                    // Do something
                    Toast.makeText(getApplicationContext(), radioButton6.getText() + " clicked", Toast.LENGTH_SHORT).show();
                    break;
            }
        }

    }
}

<强>输出:

enter image description here

希望这会有所帮助〜