I check CheckBox and TextView together, but it does not work correctly

0

I know there are many questions about checking if CheckBox is selected or if TextView is empty, but I try to do a check of both together and do not do it, what I have done is the following.

btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // compruebo si opc1 u opc2 está seleccionado y si openCalendario no está vacío
                if (opc1.isChecked() || opc2.isChecked() || verificarCampoFecha()) {
                    // solamente ir si opc1 u opc2 está seleccionado y openCalendario no está vacío
                    Intent c4c5a = new Intent(Clase4.this, Clase5.class);
                    startActivity(c4c5a);
              } else {
                    // opc1 opc2, si alguno de los dos no está seleccionado, mostrar:
                    Toast.makeText(Clase4.this, "Por favor, seleccione una franja horaria", Toast.LENGTH_SHORT).show();
                    // verificarCampoFecha, si openCalendario está vacío mostrar:
                if (openCalendario.getText().toString().equals("")) {
                     Toast.makeText(Clase4.this, "Por favor, seleccione un día", Toast.LENGTH_SHORT).show();
               }
            }
       }
   });

// compruebo openCalendario

  private boolean verificarCampoFecha() {
        if (openCalendario.getText().toString().equals("")) {
            return false;
        }
        return true;
    }

The problem is that if TextView is empty and CheckBox unselected, it only shows the Toast of CheckBox and if the CheckBox is selected, it goes to the next Activity without taking into account if TextView is empty or not.

    
asked by UserNameYo 03.06.2017 в 16:09
source

1 answer

1

Good morning campaña @UserNameYo, as I see from the comment that is in the code you need to do:

 // compruebo si opc1 u opc2 está seleccionado y si openCalendario no está vacío

with this code you are asking this:

// compruebo si opc1 u opc2 está seleccionado u openCalendario no está 
    if (opc1.isChecked() || opc2.isChecked() || verificarCampoFecha())

to do what is put in the commentary, put an "and" "& &" the code would stay something like this:

if ((opc1.isChecked() || opc2.isChecked()) && (verificarCampoFecha()))

I hope I helped you.

Added to answer.

Regarding the toast of the code section.

// opc1 opc2, si alguno de los dos no está seleccionado, mostrar:
  Toast.makeText(Clase4.this, "Por favor, seleccione una franja horaria", Toast.LENGTH_SHORT).show();

here we would have to put the message if this condition is fulfilled something like:

if (!opc1.isChecked() && !opc2.isChecked()){
// opc1 opc2, si alguno de los dos no está seleccionado, mostrar:
  Toast.makeText(Clase4.this, "Por favor, seleccione una franja horaria", Toast.LENGTH_SHORT).show();
}
    
answered by 03.06.2017 / 16:35
source