我怎样才能使RadioButtons成为RadioGroup的孙辈呢? [英] How can I put RadioButtons as grandchildren from their RadioGroup a good way?

查看:124
本文介绍了我怎样才能使RadioButtons成为RadioGroup的孙辈呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以这种方式安排了4个RadioButtons:

I have 4 RadioButtons arranged this way:

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

    <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>

    </RadioGroup>

</RelativeLayout>

我的问题是,现在每个RadioButton都可以被按下并一起选择.那不应该发生的.必须始终选择一个或零个RadioButtons,而不是多个.在普通的RadioGroup中,不会发生此问题.我认为是因为其中包含LinearLayout's,但我的其余应用程序仍需要它们.我该如何解决这个问题?

My problem is, now every RadioButton can be pressed and be selected together. That shouldn't happen. There must always be one or zero RadioButtons selected, not multiple. In a normal RadioGroup, this problem doesn't occur. I think it's because of the LinearLayout's in it, but I need them for the rest of my app. How can I solve this problem?

我怎样才能把RadioButtons作为他们的RadioGroup的孙子孙女呢?

How can I put RadioButtons as grandchildren from their RadioGroup a good way?

推荐答案

RadioGroup中使用LinearLayout时,RadioGroup会失去其基本的functionalitytoggle按钮states以及其覆盖永远不会调用方法onCheckedChanged().

As you are using LinearLayout inside RadioGroup, RadioGroup loses its basic functionality to toggle button states and also its override method onCheckedChanged() never be called.

要解决此问题,必须更改RadioButton状态programmatically.

To solve this problem, you have to change the RadioButton states programmatically.

这是一个完整的解决方案:

public class RadioGroupActivity extends AppCompatActivity implements RadioButton.OnCheckedChangeListener {

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

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

        getSupportActionBar().setTitle("Hello Android");

        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);

        radioButton1.setOnCheckedChangeListener(this);
        radioButton2.setOnCheckedChangeListener(this);
        radioButton3.setOnCheckedChangeListener(this);
        radioButton4.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);

                    // 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);

                    // 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);

                    // 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);

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

    }
}

输出:

希望这会有所帮助〜

这篇关于我怎样才能使RadioButtons成为RadioGroup的孙辈呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆