How do I make a condition to mark everything or dial a single RadioButton? Android Studio

1

I want to make an app that gives me an option to mark all radiobutton and another option that lets me just press one, so I made a radiogroup and I thought about enabling it to mark only one, and disable it when I want it to mark everything, but when I did the code in disable it did not let me mark anything ... My XML code:

<RadioGroup
    android:id="@+id/radiogroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="138dp"
    android:scrollbarSize="20dp">

    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_weight="1"
        android:text="Radio 1"
        android:textSize="30dp" />

    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:layout_weight="1"
        android:text="Radio 2"
        android:textSize="30dp" />
</RadioGroup>

<Button
    android:id="@+id/enablebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="109dp"
    android:text="Establecer is Enabled" />

<Button
    android:id="@+id/buttondisable"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="31dp"
    android:text="Disable" />

My Java code:

import android.graphics.Color;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity {

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

        final RadioGroup radiogroup = (RadioGroup) findViewById(R.id.radiogroup);

        final Button changeButton = (Button) findViewById(R.id.enablebutton);
        final Button disable= findViewById(R.id.buttondisable);

        //Habilitar y deshabilitar RADIOGROUP

        for (int i = 0; i < radiogroup.getChildCount(); i++) {
            ((RadioButton) radiogroup.getChildAt(i)).setEnabled(false);
        }


        for (int i = 0; i < radiogroup.getChildCount(); i++) {
            ((RadioButton) radiogroup.getChildAt(i)).setEnabled(true);
        }


        //Boton para activar Radiogroup

        changeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setRadioGroupEnabled(radiogroup, true);//enable RadioGroup


            }
        });

        //Boton para desactivar radiogroup
        disable.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setRadioGroupEnabled(radiogroup, false);

            }
        });



    }
    //Metodo de colocar enabled el Radiogroup

    private void setRadioGroupEnabled(RadioGroup radiogroup, boolean b) {


        for (int i = 0; i < radiogroup.getChildCount(); i++) {
            ((RadioButton) radiogroup.getChildAt(i)).setEnabled(b);
        }

    }

}
    
asked by Juanse Portillo 09.05.2018 в 16:25
source

1 answer

1

It is important to know that the use of RadioGroup is specified for multiple exclusion, therefore only one element within the group can be selected:

  

RadioGroup : This class is used to create a scope of multiple exclusion for a   set of option buttons. By dialing a radio button that   belongs to a radio group, any option button is unchecked   previously selected within the same group. Initially, all   the radio buttons are unchecked. Although it is not possible   uncheck a particular radio button, the radio group can   be removed to eliminate the verified status.

Therefore you can select a radioButton using the setChecked(true) method, since setEnabled() is used to enable / disable the view.

But only one element could be selected, even using the following loop:

 for (int i = 0; i < radiogroup.getChildCount(); i++) {
     ((RadioButton) radiogroup.getChildAt(i)).setChecked(true);
 }

would get the last selected item contained within the RadioGroup .

If you want to select multiple items, you would only use RadioButtons but not contained within a RadioGroup or you can use Checkbox which is intended for this purpose:

    
answered by 09.05.2018 в 16:45