jeudi 2 juillet 2020

Android CheckBox and Event Listener

This is a simple checkbox

public class MainActivity extends AppCompatActivity {
    private CheckBox checkBox;
    @Override
    protected void onCreate ( Bundle savedInstanceState ) {
        super.onCreate (savedInstanceState);
        setContentView (R.layout.activity_main);
        checkBox = findViewById (R.id.checkBox);
        checkBox.setOnClickListener (new View.OnClickListener () {
            @Override
            public void onClick ( View v ) {
                if (((CheckBox)v).isChecked ()){
                    Toast.makeText (MainActivity.this,"Checkbox SELECTED", Toast.LENGTH_SHORT).show ();
                }else{
                    Toast.makeText (getApplicationContext (),"Checkbox NOT SELECTED", Toast.LENGTH_SHORT).show ();
                }
            }
        });
    }
} 

Why I can't use checkBox (instance object)

if (((checkBox)v).isChecked

and i must use the class CheckBox? Are there another ways to do that? and if i have several checkbox how can identify which is checked, if i can pass a generic class?

Thanks




Aucun commentaire:

Enregistrer un commentaire