如何使用黄油刀绑定到组合框?

时间:2019-11-15 16:34:16

标签: android butterknife

使用Butterknife,如何声明选择或取消选择组合框时调用的方法?使用@OnItemSelected会得到ClassCastException

java.lang.ClassCastException: androidx.appcompat.widget.AppCompatCheckBox 
                              cannot be cast to android.widget.AdapterView

1 个答案:

答案 0 :(得分:0)

更新:

最好像这样使用@OnCheckedChanged

    @OnCheckedChanged(R.id.myCheckBox)
    void myCheckBoxSelected(boolean checked) {
        // use checked here
    }

优点是您可以立即获得布尔标志。

原始答案:

您需要使用@OnClick批注:

    @OnClick(R.id.myCheckBox)
    void myCheckBoxSelected(CheckBox checkBox) {
        boolean checked = checkBox.isChecked();
        // use checked here
    }

还请确保使用isChecked来了解已检查状态(不要使用组合框上也存在的isSelected()