ButterKnife的使用
引入,在app的build.gradle里面添加:
dependencies {
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
使用
为字段或方法添加注解,字段或方法不能使用private或static修饰
@BindView(R.id.startService) Button startService; @OnClick(R.id.startService) public void startService(View view) { goToActivity(StartServiceActivity.class); }在onCreate()里面的setContentView(R.layout.activity_main);后面添加ButterKnife.bind(this);
字段或方法使用private或static修饰时编译不能通过,提示:@BindView fields must not be private or static
3.可以直接使用变量startService了,点击按钮会调用startService方法了。startService(View view)中参数也可以不要。
本文介绍如何在Android项目中引入ButterKnife库,并演示如何使用注解简化视图绑定及事件处理过程。通过实例展示了@BindView和@OnClick注解的具体应用。
1万+

被折叠的 条评论
为什么被折叠?



