- add dependencies
dependencies {
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'}
- Or download an initialization plugin
File -> Settings -> Plugins -> Search ButterKnife, find Android ButterKnife Zeleany or Plugin Plus.
Right-click on R.layout.activity_main and select Generate -> Generate ButterKnife injections, you can choose to generate annotations corresponding to the view, and also support ViewHolder and OnClick
Manually bind ButterKnife (in activity)
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_broadcast); //Initialize the butterknife frame to be placed after setView() ButterKnife.bind(this); btn.setText("The file has been initialized"); }
//Bind the control, eliminating the repetitive operation of writing findviewbyid //Note that the method must not be private or static @BindView(R.id.btn) Button btn;
//Note that the method must not be private or static @OnClick(R.id.btn) public void onClick(View view) { btn.setText("onclick"); }