mardi 9 janvier 2018

Checkbox values not displaying in toast

I have three checkboxes and I want their values to get displayed on toast . I have written the following code . It is not working correctly . Whenever I check any checkbox , the toast doesn't comes up .

 CheckBox c1,c2,c3;
 Button b1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    c1 = (CheckBox)findViewById(R.id.c1);
    c2 = (CheckBox)findViewById(R.id.c2);
    c3 = (CheckBox)findViewById(R.id.c3);
    b1 = (Button)findViewById(R.id.b1);


    b1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if(c1.isChecked()) {
                String s1 = c1.getText().toString();
                Toast.makeText(MainActivity2.this, s1, Toast.LENGTH_SHORT);
            }


            else if(c2.isChecked()) {
                String s2 = c2.getText().toString();
                Toast.makeText(MainActivity2.this, s2, Toast.LENGTH_SHORT);
            }

            else if(c3.isChecked()) {
                String s3 = c3.getText().toString();
                Toast.makeText(MainActivity2.this, s3, Toast.LENGTH_SHORT);
            }

        }
    });
}
}




Aucun commentaire:

Enregistrer un commentaire