How to set a Boolean value of a RadioGroup

1

Example:

EditText etNome      =  (EditText) findViewById(R.id.etName);
EditText etAge       =  (EditText) findViewById(R.id.etAge);
RadioGroup rgClienteVivo = (RadioGroup) findViewById(R.id.rgClienteVivo);

If I wanted to set these two values it would look like this:

usuario.setNome(etNome.getText().toString());
usuario.setAge(etAge.getText().toString());

How would that look like RadioGroup ?

    
asked by anonymous 02.10.2016 / 03:37

1 answer

1

The RadioGroup manages a set of RadioButton where, when one is selected, deselects the previously selected one.

You can select any of the RadioButton within the RadioGroup in two ways:

  • via R.id of button

    mRadioGroup.check(R.id.radioButton1);
    
  • By position (index) that it occupies in RadioGroup

    RadioButton radioButton = (RadioButton)mRadioGroup.getChildAt(indice);
    radioButton.setChecked(true);
    

To deselect all the buttons use:

mRadioGroup.clearCheck();

To get R.id of the selected button use:

int id = mRadioGroup.getCheckedRadioButtonId();

To run certain code based on the selected button use:

int id = mRadioGroup.getCheckedRadioButtonId();

switch(id){

    case R.id.radioButton1:
        ...
        ...
    break;
    case R.id.radioButton2:
        ...
        ...
    break;
}
    
02.10.2016 / 11:51