C check when a RadioButton has not been selected

0

I am doing a questionnaire, but I can not achieve that, when in a RadioGroup determined none of the options are marked, a Toast appears with an error message, I already have the Toast created, and the variables also, but I can not find the way to create the system.

Here is my code and my XML.

Button sig = (Button) findViewById(R.id.env); // cargo el boton
    sig.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Context context = getApplicationContext();
            int duration = Toast.LENGTH_SHORT;
            EditText nom = (EditText) findViewById(R.id.editText3);
            EditText pci = (EditText) findViewById(R.id.editText2);
            EditText pcu = (EditText) findViewById(R.id.editText);
            RadioGroup grupoun = (RadioGroup) findViewById(R.id.Grupo1);
            RadioGroup grupodo = (RadioGroup) findViewById(R.id.Grupo2);
            RadioGroup grupotr = (RadioGroup) findViewById(R.id.Grupo3);
            String strNombre = nom.getText().toString();
            String strPregCinco = pci.getText().toString();
            String strPregCuatro = pcu.getText().toString();

            if(strNombre.matches("") || strPregCinco.matches("") || strPregCuatro.matches(""))
            {
                CharSequence text2 = "¡Dejaste campos vacíos o no marcaste algunas opciones!";
                Toast error = Toast.makeText(context, text2, duration);
                error.show();
            }
            else
            {
                Intent pas = new Intent(encuesta.this, MainActivity.class);
                CharSequence text = "¡Encuesta enviada!";
                Toast toast = Toast.makeText(context, text, duration);
                toast.show();
                startActivity(pas);
            }
        }

    });
<RadioGroup
        android:id="@+id/Grupo1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <RadioButton
            android:id="@+id/radioButton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1/2 hora a 1 hora" />

        <RadioButton
            android:id="@+id/radioButton1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1 hora o 2 horas" />

        <RadioButton
            android:id="@+id/radioButton2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2 horas o más" />
    </RadioGroup>

<TextView
    android:text="2. ¿Cuánto tiempo tarda desde el colegio a su casa?"
    android:layout_marginTop="20dp"
    android:textSize="16dp"
    android:textColor="#000000"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/Grupo2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1/2 hora a 1 hora" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="1 hora o 2 horas" />

        <RadioButton
            android:id="@+id/radioButton5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="2 horas o más" />
    </RadioGroup>

<TextView
    android:text="3. ¿Cuáles de las siguientes zonas de Bogotá está localizado su domicilio?"
    android:layout_marginTop="20dp"
    android:textSize="16dp"
    android:textColor="#000000"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/textView2" />

    <RadioGroup
        android:id="@+id/Grupo3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <RadioButton
        android:id="@+id/radioButton6"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Norte de Bogotá (calle 72 en adelante)" />

        <RadioButton
            android:id="@+id/radioButton7"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Suba (noroccidente)" />

        <RadioButton
            android:id="@+id/radioButton8"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Centro - Chapinero - Teusaquillo" />

        <RadioButton
            android:id="@+id/radioButton9"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Alrededores de Bogotá" />

        <RadioButton
            android:id="@+id/radioButton10"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Sur de Bogotá" />

        <RadioButton
            android:id="@+id/radioButton11"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Occidente (Puente Aranada - Fontibón - Engativá)" />
    </RadioGroup>
    
asked by Pablo Gonzalez 06.03.2017 в 00:19
source

1 answer

1

If you only want to check a specific RadioButton you can use the following:

RadioButton radioButton = (RadioButton) findViewById(R.id.radio);

if(radioButton.isChecked())
{
  // Está marcado el radio "radioButton" 
}

If you want to check if any of the RadioButton of a RadioGroup has been selected, use the following:

RadioGroup grupoun = (RadioGroup) findViewById(R.id.Grupo1);

if (grupoun.getCheckedRadioButtonId() == -1)
{
  // No hay ningun radio seleccionado
}
    
answered by 06.03.2017 в 18:52