1

Quiero obtener el texto del rabiobutton seleccionado por el usuario. Tengo 5 radiobutton dentro de un radiogroup para luego utilizar este campo en un Asynctask e insertar en una BD. el caso es que no puedo obtener el texto.

Lo que tengo es lo siguiente

        radioGroup=(RadioGroup) findViewById(R.id.radiog1);
        rbmuybueno=(RadioButton) findViewById(R.id.rbmuybien);
        rbbueno=(RadioButton) findViewById(R.id.rbbueno);
        rbregular=(RadioButton) findViewById(R.id.rbregular);
        rbmalo=(RadioButton) findViewById(R.id.rbmala);
        rbmuymalo=(RadioButton)findViewById(R.id.rbmuymala);
public void  validarradiobutton(){
    if (rbmuybueno.isChecked() || rbbueno.isChecked() || rbmalo.isChecked() || rbregular.isChecked()  || rbmuymalo.isChecked())
    {

            final String pregunta=preguntaa.getText().toString();


    }
    else
    {
        final AlertDialog.Builder alertaDeError2 = new AlertDialog.Builder(CalificarEvento.this);
        alertaDeError2.setTitle("Advertencia");
        alertaDeError2.setMessage("Selecione una alternativa para continuar.");
        alertaDeError2.setPositiveButton("Entendido", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        });
        alertaDeError2.create();
        alertaDeError2.show();

    }

necesito obtener el texto dentro del IF para luego pasarle los parámetros al Asynctask

1

1 respuesta 1

5

Si tienes un RadioButton para obtener el texto sería:

 rbmuybueno=(RadioButton) findViewById(R.id.rbmuybien);
 String texto = rbmuybueno.getText().toString();

Si tienes un RadioGroup que contiene RadioButtons, primeramente obtendrías el id del RadioGroup:

int radioButtonId = radioButtonGroup.getCheckedRadioButtonId();

obtendrías una referencia del elemento por medio del id:

View radioButton = radioButtonGroup.findViewById(radioButtonId);

y obtendrías el indice del RadioButton dentro del RadioGroup:

int indice = radioButtonGroup.indexOfChild(radioButton);

Para obtener el texto del RadioButton:

 RadioButton rb = (RadioButton)  radioButton.getChildAt(indice);
 String texto = rb.getText().toString();
1
  • Gracias y disculpa por no responder antes, ya he votado la respuesta ...
    – Ashley G.
    el 12 ene. 2017 a las 13:39

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.