天天看点

Android chexkbox取消自动切换选中状态

@SuppressLint("AppCompatCustomView")
public class MyCheckBox extends CheckBox {
    public MyCheckBox(Context context) {
        super(context);
    }

    public MyCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void toggle() {

    }

/***
*  自定义checkbox  重写performClick方法,CompoundButton的performClick方法中调用了  toggle方法,只要把这个方法去掉就可以
*了,或者将toggle方法重写去掉super方法,均可以防止点击checkbox自动改变选中状态
*
**/
    @Override
    public boolean performClick() {
        final boolean handled = super.performClick();
        if (!handled) {
            // View only makes a sound effect if the onClickListener was
            // called, so we'll need to make one here instead.
            playSoundEffect(SoundEffectConstants.CLICK);
        }

        return handled;
    }
}