Change Checkbox value without triggering onCheckChanged

前端 未结 19 1054
广开言路
广开言路 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:32

    I guess using reflection is the only way. Something like this:

    CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
    try {
        Field field = CompoundButton.class.getDeclaredField("mChecked");
        field.setAccessible(true);
        field.set(cb, cb.isChecked());
        cb.refreshDrawableState();
        cb.invalidate();
    } catch (Exception e) {
        e.printStackTrace();
    }
    

提交回复
热议问题