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

三世轮回 提交于 2019-12-12 03:27:55

问题


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>

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?

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


回答1:


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.

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

Here is a complete solution:

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

    }
}

OUTPUT:

Hope this will help~




回答2:


I will suggest you not to increase View Hierarchy by using nested Nested Layout (Linear/Relative) inside a RadioGroup. Also you will not get single selection feature using Nested Layout. RadioGroup actually extends LinearLayout. So it has only the capability of arranging RadioButtons either Vertically or Horizontally. Here I shared the link RelativeRadioGroup of my Library which is actually a RelativeRadioGroup so that you can arrange RadioButtons as you wish and specially selection will not be affected.




回答3:


You can't put any other layout in RadioGroup. So if you still want to put all your RadioButtons in Linear layout, You'll have to do little extra work.

Do it programmatically, When any RadioButton is checked set others unchekced.

Something like

RadioButton rbu1 =(RadioButton)findViewById(R.id.radio0);
RadioButton rbu2 =(RadioButton)findViewById(R.id.radio1);
rbu1.setOnClickListener(first_radio_listener);
rbu2.setOnClickListener(second_radio_listener);
OnClickListener first_radio_listener = new OnClickListener (){
public void onClick(View v) {
  rbu2.setChecked(false);
 }
};
OnClickListener second_radio_listener = new OnClickListener (){
public void onClick(View v) {
  rbu1.setChecked(false);
 }
};


来源:https://stackoverflow.com/questions/43563150/how-can-i-put-radiobuttons-as-grandchildren-from-their-radiogroup-a-good-way

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!