Problems with the toast in android studio

2

I am developing a small app that will show me a Toast when I click on a button.

The problem is that I run the normal app but it does not show me the toast and it does not throw me an error either, it's as if I had not put it. I would appreciate the help of someone thanks.

public class MainActivity extends AppCompatActivity {

    CheckBox c1;
    CheckBox c2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c1=(CheckBox) findViewById(R.id.idcheck1);
        c2= (CheckBox) findViewById(R.id.idcheck2);

    }

    public void  onClick(View view){
        if(view.getId()==R.id.btn1){
            validar();
        }
    }


    private void validar(){
        String cad="Seleccionado:\n";
        if (c1.isChecked()==true){
            cad+="Opcion 1";
        }
        if(c2.isChecked()==true){
            cad+="Opcion 2";
        }
        Toast.makeText(getApplicationContext(),cad , Toast.LENGTH_SHORT).show();
    }

}
    
asked by Daniel Sanchez Sepulveda 22.10.2018 в 07:20
source

1 answer

1

The onClick() method is not called because you are not defining the listener to your Activity,

   public void  onClick(View view){
        if(view.getId()==R.id.btn1){
            validar();
        }
    }

In order for this method to be called, your Activity class must implement View.OnClickListener like this:

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

In this way you can define the listener to the element that would call the click:

 Button boton = findViewById(R.id.btn1); 
 //Define listener.
 boton.setOnClickListener(this);

and would call the method

 onclick()

This would be the complete code:

public class MainActivity extends AppCompatActivity  implements View.OnClickListener{

    CheckBox c1;
    CheckBox c2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        c1=(CheckBox) findViewById(R.id.idcheck1);
        c2= (CheckBox) findViewById(R.id.idcheck2);

        Button boton = findViewById(R.id.btn1); 
        //Define listener.
        boton.setOnClickListener(this);

    }

    public void  onClick(View view){
        if(view.getId()==R.id.btn1){
            validar();
        }
    }


    private void validar(){
        String cad="Seleccionado:\n";
        if (c1.isChecked()==true){
            cad+="Opcion 1";
        }
        if(c2.isChecked()==true){
            cad+="Opcion 2";
        }
        Toast.makeText(getApplicationContext(),cad , Toast.LENGTH_SHORT).show();
    }

}

If you do not want your class Activity to implement the listener View.OnClickListener simply define the listener in the view :

 Button boton = findViewById(R.id.btn1); 
 boton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {

                 validar();

                }
            });
    
answered by 22.10.2018 в 20:08