How to define a boolean value of a Radiogroup

Asked

Viewed 54 times

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 those two values it would be like this:

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

How would the RadioGroup?

1 answer

1

The Radiogroup manages a set of Radiobutton where, when one is selected, the selected.

It is possible to select any of the Radiobutton within the Radiogroup in two ways:

  • through the R.id button

    mRadioGroup.check(R.id.radioButton1);
    
  • through the position (index) which it occupies in the Radiogroup

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

To deselect all buttons use:

mRadioGroup.clearCheck();

To obtain the R.id from the selected button use:

int id = mRadioGroup.getCheckedRadioButtonId();

To execute certain code depending on the selected button use:

int id = mRadioGroup.getCheckedRadioButtonId();

switch(id){

    case R.id.radioButton1:
        ...
        ...
    break;
    case R.id.radioButton2:
        ...
        ...
    break;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.