简体   繁体   中英

Checkbox not setChecked not smoothly

I have a CheckBox that I set OnCheckedChangeListener for it and in the onCheckedChange method I have heavy code. When I want to set Checkbox.checked it gets checked with a little delay. I want it to set checked smooth with no delay I used post and runnable in onCheckedChange method but it wasn't changed.

Try to use threads , I think it'll help

new Thread(new Runnable() { 
        public void run(){        
            // Your heavy code
        }
    }).start();

You can use setOnClickListener , in onClick method write your code.. To get the status of checkbox, you can use isChecked() method which returns boolean value.

c.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            boolean checked = ((CheckBox) v).isChecked();
            switch (v.getId()) {

                case R.id.checkboxid:

                    if (checked) {
                        //true case
                    } else {
                        //false case
                    }
                    break;
            }
        }
    });

Thread and post on View didn't help,but postDelayed helped me

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
   final int id = buttonView.getId();


    buttonView.postDelayed(new Runnable() {
        @Override
        public void run() {

           //hard code


            }


        }
    },400);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM