Change Checkbox value without triggering onCheckChanged

前端 未结 19 998
广开言路
广开言路 2020-11-30 00:18

I have setOnCheckedChangeListener implemented for my checkbox

Is there a way I can call

checkbox.setChecked(false);
         


        
19条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-30 00:46

    For anyone that stumbles across this, one simpler way to do this is to just use a tag on the checkbox and then check that tag on its listener (code is in Kotlin):

    checkBox.tag = false
    checkBox.setOnCheckedChangeListener{ buttonView, isChecked -> 
        if(checkBox.tag != true) {
            //Do some stuff
        } else {
            checkBox.tag = false
        }
    

    Then when accessing just set the tag to true before you set the isChecked to true when you want to ignore the value change:

    checkBox.tag = true
    checkBox.isChecked = true
    

    You could also map the tag to a key by using the alternative setTag method that requires a key if you were worried about understandability. But if its all contained to a single class a few comment strings will be more than enough to explain whats happening.

提交回复
热议问题