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;
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/android-butterknefe-record/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
Android Butterknife 采坑记录
子module中使用 在Library module中使用参考:https://github.com/JakeWharton/butterknife#library-projects Now make sure you use R2 instead of R inside ……
<<上一篇
下一篇>>
文章目录
关闭
目 录