Android Butterknife 采坑记录

子module中使用

在Library module中使用参考:https://github.com/JakeWharton/butterknife#library-projects

Now make sure you use R2 instead of R inside all Butter Knife annotations.

class ExampleActivity extends Activity {
    @BindView(R2.id.user)
    EditText username;
    @BindView(R2.id.pass)
    EditText password;

    @OnClick({R2.id.btn_alipay, R2.id.btn_wechat})
    void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_alipay) {

        } else if (id == R.id.btn_wechat) {

        }
    }
}

成员变量不能使用private修饰

@BindView fields must not be private or static.

参考:https://github.com/JakeWharton/butterknife/issues/518

  • 错误姿势
@BindView(R2.id.btn_success)
private Button btnSuccess;
@BindView(R2.id.btn_failure)
private Button btnFailure;
  • 正确姿势
@BindView(R2.id.btn_success)
Button btnSuccess;
@BindView(R2.id.btn_failure)
Button btnFailure;

Fragment中绑定

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(this, view);
    initView(view);
    return view;
}
上一篇 Android Studio 3.4 自定义注解处理器不能自动生成文件解决
下一篇 Android DataBinding 采坑记录
目录
文章列表
1 Android NDK引用Lame动态链接库
Android NDK引用Lame动态链接库
2
Spring Boot 2.X返回json数据中null字段不显示解决方法
Spring Boot 2.X返回json数据中null字段不显示解决方法
3
Eureka状态变更的接口
Eureka状态变更的接口
4
Node.js包管理工具yarn安装使用
Node.js包管理工具yarn安装使用
5
ElasticSearch 7 学习(2)索引基本操作
ElasticSearch 7 学习(2)索引基本操作
最新评论
一位WordPress评论者
一位WordPress评论者
2月12日
您好,这是一条评论。若需要审核、编辑或删除评论,请访问仪表盘的评论界面。评论者头像来自 Gravatar。