如果直接在App中使用,那么只需要在app的build.gradle中添加即可。
android { ... // Butterknife requires Java 8. compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } } dependencies { implementation 'com.jakewharton:butterknife:10.2.0' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.0' }
使用时,请在目标activity上调用 ButterKnife.bind(this); 用来绑定ButterKnife和Activity
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); }
具体使用:
绑定按钮
@OnClick(R.id.btn_butterknife) public void onClicked(View view) { switch (view.getId()) { case R.id.btn_butterknife: Log.d("Demo", "" + R.id.btn_butterknife); break; } }
绑定文本输入框
@BindView(R.id.tv_butterknife)
TextView tv_butterknife;
tv_butterknife.setText("demo123");