Get tired of writing findViewById. Try ButterKnife, butterknife.
Go to the official website first
Http://jakewharton.github.io/butterknife/ and https://github.com/JakeWharton/butterknife
Configure the development environment
Before the code starts, add the Library to the dependency.
Eclipse
Go to the official website to manually download the jar package and put it in the libs directory or add it to Build Path in other ways.
Android StudioGUI operations
File-> Project Structure on the menu (or directly click the Project Structure on the toolbar) -> app under Modules on the left-> Dependencies tab on the right-> + number-> Library dependency search butterknife select com. jakewharton: butterknife: 8.4.0 and then OK. The version number will change. If there is no-compiler, it will have a tail.
AnnotationProcessor is added to the new butterknife, which cannot be added. Therefore, you must edit the Gradle Script.
How to edit Gradle Script
Open build. gradle of the Module app and add two lines in dependencies.
dependencies { ... compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' ...}
The ellipsis represents other existing dependencies. Sync after adding
Start ButterKnife
Coding starts after the development environment is configured
To use in Activity, you must first start butterknife. bind (this) immediately after setContentView in onCreate );
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); }
Bind View
Replace findViewById with @ BindView
public class MainActivity extends AppCompatActivity { @BindView(R.id.btnGet) Button mBtnGet; @BindView(R.id.tvResult) TextView mTvResult; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); mBtnGet.setText("do get"); }}
Note:Annotations can only be used on class members, and Members cannot be modified using private or static annotations.And cannot be used on local variables in the method.
Bind Resources
@BindString(R.string.app_name) String appName; @BindColor(R.color.colorPrimary) int colorPrimary; @BindBool(R.bool.bool_name) boolean boolName;
More types are supported, so classes are not listed one by one.
Bind a click event
You do not need to declare the view or setOnClickListener. The parameter is optional.
@OnClick(R.id.btnPost) void doPost() { mTvResult.setText("do post done"); }
You can also include parameters like onClickListener.
@OnClick(R.id.btnPost) void doPost(View view) { Button btnPost = (Button)view; mTvResult.setText("do post done " + btnPost.getText().toString()); }
You can also save the strong conversion, and directly use the exact type of butterknife on the parameters to help you with automatic transformation.
@OnClick(R.id.btnPost) void doPost(Button btnPost) { mTvResult.setText("do post done " + btnPost.getText().toString()); }
Sending