Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

disable checkbox after checked, android

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

like image 214
cgpa2.17 Avatar asked Oct 03 '12 04:10

cgpa2.17


1 Answers

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 
      }
    }    
});
like image 144
Hiral Vadodaria Avatar answered Oct 02 '22 19:10

Hiral Vadodaria