samedi 14 mars 2015

If a specific checkbox is check do something, if any other checkbox is clicked do something else

I am creating this activity that I request the user to find the correct answer using check boxes. My layout has multiple checkboxes but one of them is correct one. The main idea is to give a user 3 tries to guess the correct answer( the one checkbox). I manage to implement a code that when the correct checkbox is clicked to do something, but I can't get the code to work when user is clicking on any other checkbox.


My code is below (currently implemented the correct checkbox, but code for any other checkbox does not work)



int counts = 0;

checkBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (((CheckBox) v).isChecked()){
DisplayToast("Correct Answer");
Intent in = new Intent(getApplicationContext(),MainActivity.class);
startActivity(in);
}
else{
counts ++;
DisplayToast("Incorrect Answer" + counts);
}
}
});


Open to any suggestions of how to do the code to do something in case another checkbox is clicked


I also know that this code exists for this purposes



RadioGroup radioGroup = (RadioGroup) findViewById(R.id.rdbGp1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb1 = (RadioButton) findViewById(R.id.rdb1);
if (rb1.isChecked()) {
DisplayToast("Option 1 checked!");
} else {
DisplayToast("Option 2 checked!");
}
}
});


Any similar for checkboxes?





Aucun commentaire:

Enregistrer un commentaire