Use of the "Android-frame" butterknife

Source: Internet
Author: User

Butterknife can eliminate the steps of control Findviewbyid and improve development efficiency.

Usage:

1. Add Dependencies:

Compile ' com.jakewharton:butterknife:5.1.1 '

2. Use in activity:

 Public classMainactivityextendsappcompatactivity {//use Butterknife to initialize the control (eliminating the use of the Findviewbyid () method, which improves development efficiency)@InjectView (r.id.tv)protectedTextView TV; @InjectView (R.ID.IV)protectedImageView IV; @InjectView (R.ID.BTN)protectedbutton button; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //Register ButterknifeButterknife.inject ( This); Tv.settext ("Hello TextView");    Iv.setimageresource (R.mipmap.ic_launcher); }    //trigger event in the Click () method when r.id.btn represents a control@OnClick (R.ID.BTN)protected voidClick () {Tv.settext ("Button Clicked"); }}

3. Use in Fragment:
 Public classMainfragmentextendsFragment {//use Butterknife to initialize the control (eliminating the use of the Findviewbyid () method, which improves development efficiency)@InjectView (r.id.tv) TextView TV;    @InjectView (R.ID.IV) ImageView IV;    @InjectView (R.ID.BTN) button button; @Nullable @Override PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancestate) { View View= Inflater.inflate (R.layout.activity_main, container,false); //Register Butterknife (use Butterknife in fragment to specify a root View)Butterknife.inject ( This, view); returnview; } @Override Public voidonviewcreated (view view, @Nullable Bundle savedinstancestate) {Tv.settext ("Hello Butterknife");    Iv.setimageresource (R.mipmap.ic_launcher); }    //trigger event in the Click () method when r.id.btn represents a control@OnClick (R.ID.BTN) Public voidClick () {Tv.settext ("Button Clicked"); }}

Note: No matter the method or the control, as long as the annotation butterknife, you can not use private or static decoration!!

Use of the "Android-frame" 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.