Reputation: 319
I want to achieve this : user check on a unchecked checkbox, a toast displayed, the checkbox then become disable..
male.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked){
Toast.makeText(CheckBoxTuts.this, "male" , Toast.LENGTH_SHORT).show();
male.setChecked(false);
}
}
});
the output failed, because they execute on the same time, even I put the male.setChecked(false) outside.. I can't recall there's something to run something 1st, then other thing.. is it thread? really cant remember
Upvotes: 21
Views: 56914
Reputation: 11
First , Register Your CheckBox to OnCheckedChangeListener.
chk.setOnCheckedChangeListener(this);
then, Overrides it's Method
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b)
{
if (chk.isChecked())
{
chk.setEnabled(false);
}
}
Upvotes: 1
Reputation: 407
Try to use .onClickListener(View.OnClickListener)
with implemented View.OnClickListener
. In body of implemented method you will check if the CheckBox is checked or not and set them.
Upvotes: 2
Reputation: 19250
If you want to achieve: "user check on a unchecked checkbox, a toast displayed, the checkbox then become disable..",you should try this code:
male.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked){
Toast.makeText(CheckBoxTuts.this, "male" , Toast.LENGTH_SHORT).show();
male.setEnabled(false); // disable checkbox
}
}
});
Upvotes: 40
Reputation: 33534
Try this....
- Use the setEnabled(false)
on the CheckBox
male.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
if (isChecked){
Toast.makeText(CheckBoxTuts.this, "male" , Toast.LENGTH_SHORT).show();
male.setEnabled(false); // Will Disable checkbox
}
}
});
Upvotes: 8
Reputation: 21191
male.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
// TODO Auto-generated method stub
if (isChecked){
Toast.makeText(CheckBoxTuts.this, "male" , Toast.LENGTH_SHORT).show();
//male.setVisibility(View.GONE);//disappear your check box
male.setEnabled(false);//disable your check box
}
}
});
Upvotes: 2