Recommend an Android developer Lazy Library--Butterknife

Source: Internet
Author: User

Butterknife

--Project Address: Https://github.com/JakeWharton/butterknife

All say programmers are lazy, and everything is to let the program automation to help alleviate the workload, this open source library can let us from a large number of Findviewbyid () and Setonclicktlistener () liberation.

Liberating control Object Instantiation

That is Findviewbyid (), all along the way is defined, and then Setcontentview () or inflate () after hit Findviewbyid () to instantiate, and use Butterknife, you only need in the code Use annotations to make an object declaration, and then invoke a word after setcontentview () or inflate (), then all declared objects are automatically created.

1@InjectView (R.ID.OK_BTN)//the ID of the control corresponding to the2 Button mBtn;3 4 @InjectView (R.id.title_text)5 TextView Mtitletextview;6 7 @Override8     protected voidonCreate (Bundle savedinstancestate) {9         Super. OnCreate (savedinstancestate);Ten Setcontentview (r.layout.main_activity); One  AButterknife.inject ( This); -  -         //Then you can use the variable directly. theMtitletextview.settext ("Test"); -  -}

is not very magical!!!

Fragment and adapter can also be used, but the call should add a root view parameter.

Fragegment remember to inherit Ondestroyview at the same time, and in which it will Butterknife.reset

1  Public classFancyfragmentextendsFragment {2 @InjectView (R.id.button1) Button button1;3 @InjectView (r.id.button2) Button button2;4 5 @Override View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {6View view = Inflater.inflate (R.layout.fancy_fragment, container,false);7Butterknife.inject ( This, view);8     //TODO use "injected" views ...9     returnview;Ten   } One } A  -@OverridevoidOndestroyview () { -     Super. Ondestroyview (); theButterknife.reset ( This); -}

You can also instantiate an array of controls, annotating one S, or Injectviews

1 @InjectViews ({r.id.first_name, r.id.middle_name, r.id.last_name}) list<edittext> nameviews;

Free listening add

As below, can be directly for R.id.submit this control add Onclicklistener as a submit function, the abuse ah ...

1 @OnClick (r.id.submit) 2  Public void Submit () {3   // TODO submit data to server ... 4 }

You can also add multiple controls to the same response function in batches:

1 @OnClick ({r.id.door1, R.id.door2, R.id.door3})2    Public voidPickdoor (Doorview door) {3    if(Door.hasprizebehind ()) {4Toast.maketext ( This, "You win!", Length_short). Show ();5}Else {6Toast.maketext ( This, "Try Again", Length_short). Show ();7    }8}

Specifically, you can see the project page of the Great God. was given before the text.

Note: The latest version of the library has changed the method name, of course, is compatible with the examples listed above. The latest examples are as follows:

1 classExampleactivityextendsActivity {2 @Findview(r.id.user) EditText username;3 @Findview(r.id.pass) EditText password;4 5@OnClick (R.id.submit)voidSubmit () {6     //TODO Call Server ...7   }8 9@Override Public voidonCreate (Bundle savedinstancestate) {Ten     Super. OnCreate (savedinstancestate); One Setcontentview (r.layout.simple_activity); AButterknife.Bind( This); -     //TODO Use fields ... -   } the}

Recommend an Android developer Lazy Library--Butterknife

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.