Often hear such a slogan, called "trying to be a lazy program ape." Obviously, Android Butterknife has helped you become a lazy program ape to some extent. As an Android developer, you must be very familiar with Findviewbyid (), Setonclicklistener (), Setonclicklistener () and so on, and over time, are you tired of these things, simply not nutritious, and have to write, This time, open source frameworks such as Android Butterknife,androidannotations appear.
Butterknife new Features:
1. Support View injection in Activity
2. Support View injection in view
3. Support View Event callback function injection (@OnLongClick @OnFocusChanged @OnItemClick @OnItemLongClick @OnCheckedChanged, etc.)
Today, let's talk about the use of Android Butterknife, Android Butterknife is a focus on Android system View injection framework, can reduce at least half the amount of code.
First step, must be quoted library
1. Import Butterknife-7.0.1.jar,butterknife-7.0.1.jar
I use the Androidstudio development, if you are using eclipse, then you need to do the following configuration
2. Right-click the project, properties->java compiler→annotation processing, select Enable project specific settings.
3. Expand annotation processing, click Factory Path, select Enable project specific settings, then click Add JARs ..., select libs butter jar Package under knife
In addition to the onclick, the callback function bindings for other events are the same, and they are not described in detail. Configuration is complete, give an example, everyone will understand.
First, take a look at the XML layout file, which simply defines a few button controls
[HTML]View PlainCopy
- <linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="Http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:id="@+id/btn1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/btn2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/btn3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/btn4"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </linearlayout>
In activity, the binding of annotations.
[HTML]View PlainCopy
- Package COM.NEWLAND.ADSL;
- Import Android.support.design.widget.TextInputLayout;
- Import android.support.v7.app.ActionBarActivity;
- Import Android.os.Bundle;
- Import Android.view.View;
- Import Android.view.ViewGroup;
- Import Android.widget.BaseAdapter;
- Import Android.widget.Button;
- Import Android.widget.TextView;
- Import Android.widget.Toast;
- Import Butterknife. Bind;
- Import Butterknife. Butterknife;
- Import Butterknife. OnClick;
- public class Mainactivity extends Actionbaractivity {
- @Bind (R.ID.BTN1)
- Button Btnone;
- @Bind (R.ID.BTN2)
- Button Btntwo;
- @Bind (R.ID.BTN3)
- Button Btnthree;
- @Bind (R.ID.BTN4)
- Button Btnfour;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.activity_main);
- Butterknife.bind (this); binding annotations
- Btnone.settext ("button-1");
- Btntwo.settext ("button-2");
- Btnthree.settext ("button-3");
- Btnfour.settext ("button-4");
- }
- @OnClick (R.ID.BTN1)//without parameters
- public void Btnone () {
- Toast.maketext (Mainactivity.this, "Toast comes Out", Toast.length_short). Show ();
- }
- @OnClick (R.ID.BTN2)//with button parameter
- public void Btntwo (Button button) {
- Button.settext ("hello!");
- }
- @OnClick ({r.id.btn3, r.id.btn4})//Bind multiple controls at the same time
- public void Btnthreeandfour (Button button) {
- Button.settext ("world!");
- }
- }
In OnCreate (), after Setcontentview (), be sure not to forget the binding annotations
[Java]View PlainCopy
- Butterknife.bind (this); //binding annotations
@Bind (R.ID.BTN1) This binds the following variable name to the control ID, which is equivalent to executing the Findviewbyid ()
@OnClick (R.ID.BTN2) This is the click event of a bound control, which executes what is done when the click event is triggered, equivalent to Setonclicklistener (new Onclicklistener (OnClick (...)));
The above is the implementation of the binding under the activity, of course, in Viewholder mode and fragement can also be bound.
Binding in Viewholder mode:
[Java]View PlainCopy
- Public class Myadapter extends Baseadapter {
- @Override
- Public View GetView (int position, view view, ViewGroup parent) {
- Viewholder Holder;
- if (view! = null) {
- Holder = (viewholder) view.gettag ();
- } Else {
- view = Getlayoutinflater (). Inflate (R.layout.design_navigation_item, parent, false);
- Holder = New Viewholder (view);
- View.settag (holder);
- }
- Holder.name.setText ("Inject");
- return view;
- }
- @Override
- public long getitemid (int position) {
- return position;
- }
- @Override
- Public Object GetItem (int position) {
- return null;
- }
- @Override
- public int GetCount () {
- return 0;
- }
- class Viewholder {
- @Bind (r.id.title)
- TextView name;
- Public Viewholder (view view) {
- Butterknife.bind (this, view);
- }
- }
- }
Bindings under Fragement:
[Java]View PlainCopy
- Public class Myfragment extends Fragment {
- @Bind (R.id.button1)
- Button button1;
- @Bind (R.id.button2)
- Button button2;
- @Override
- Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
- View view = Inflater.inflate (R.layout.fragment, container, false);
- Butterknife.bind (this, view);
- //TODO use fields ...
- return view;
- }
- }
Incidentally, every activity that needs to bind annotations has to execute butterknife.bind (this); What if I'm afraid to forget? Write a baseactivity, inherit activity, in Baseactivity's OnCreate function to bind, then we just write their own activity to inherit baseactivity on the line, of course, Baseactivity can also implement some other common things.
One thing to emphasize here is that if you use Androidstudio to add compile ' com.jakewharton:butterknife:6.1.0 ' to the Butterknife directly through Build.gradle, Then there will be some changes, as follows:
[Java]View PlainCopy
- @InjectView (R.id.hello)
- TextView Hello;
- @Override
- protected void OnCreate (Bundle savedinstancestate) {
- super.oncreate (savedinstancestate);
- Butterknife.inject (this);
- }
This is the difference between butterknife:6.1.0 and Butterknife-7.0.1.jar because of the different versions.
Android Butterknife Injection Framework