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)中参数也可以不要。