The Android butterknife annotation framework Uses

Source: Internet
Author: User

This period of time to learn the next Butterknife annotation framework, learning is not particularly in-depth, but the basis is almost, in this record summary.

Butterknife is a library of Android view injections, mainly used for annotations, which reduces the writing of many code, making the code more concise and uncluttered. Butterknife can avoid Findviewbyid calls, Android developers know that when Android initializes the control object to constantly call Findviewbyid, how many controls are required to call the number of times, The use of Butterknife can eliminate the call of Findviewbyid, not only this can also save the lengthy code to listen to events, just need a note to complete. Let's take a look at how butterknife is used.

first, how to introduce Butterknife?

1. First, add dependencies in Project Gradle:

1 Dependencies {2         // Import of Butterknife 3         Classpath ' com.neenbedankt.gradle.plugins:android-apt:1.8 '4 }

2. Add the following in the app's Gradle:
Add in Gradle:

1 Apply plugin: ' Android-apt '

In the dependencies of Gradle, add:

Dependencies {    ' com.jakewharton:butterknife:8.4.0 '    Com.jakewharton: butterknife-compiler:8.4.0 '}

3. The rebuild is complete.

Here's a look at the differences between project and app Build.gradle in this article: the difference between two build.gradle configurations in Android project and app

second, how to use?

Note: The modifier type of the button cannot be: private or static. Otherwise, error: @BindView fields must not is private or static. (Com.zyj.wifi.ButterknifeActivity.button1)

(i), the binding of the view

1. Note For the control ID: @BindView ()

1 @BindView (R.id.toolbar) 2  Public Toolbar Toolbar;

Then call in the activity's OnCreate ():

1  This) ;

2. Multiple control ID annotations: @BindViews ()

1     @BindViews ({r.id.button1  , R.id.button2,  R.id.button3})2public      List <Button> buttonlist;

Then call in the activity's OnCreate ():

1  This) ;

3. Binding controls in other view
Butter Knife provides several overloads of bind that can be used with annotation bindings in any object, as long as it is passed in and out of the layout. Call the Butterknife.bind (view. This) method. However, the general call to the Unbinder unbinder=butterknife.bind (view, this) method requires a call to Unbinder.unbind () to unbind.
So I usually use the method in (iv) When I invoke the control in the activity and then bind the other controls in the view.

(ii), the binding of resources
1 <Resources>2     <stringname= "Hello">Hello</string>3     <String-arrayname= "Array">4         <Item>Hello</Item>5         <Item>Hello</Item>6         <Item>Hello</Item>7         <Item>Hello</Item>8     </String-array>9 </Resources>

1. @BindString (): Binding string string

1     @BindString (R.string.hello)2      Public String Hello;

Then call in the oncreate of the activity:

1  This) ;

2. @BindArray (): Binds a string with array arrays

1     @BindArray (R.array.array)  // bind string inside array array 2     string [] array;

Then call in the activity's OnCreate ():

1  This) ;

3. @BindBitmap (): Bind bitmap Resource

1     @BindBitmap (r.mipmap.ic_launcher)2      Public Bitmap Bitmap;

Then call in the activity's OnCreate ():

1  This) ;

6. Other Resources
Binding Bindcolor (), Binddimen (), binddrawable (), Bindint () and so on are all the same methods, (1). Binds a resource. (2). Call the Butterknife.bind () method.

(iii), the binding of events

1. Binding the OnClick method

1     @OnClick (r.id.login_activity_button_login)2      Public void Clicklogin () {3     }

Then call in the activity's OnCreate ():

1  This) ;

If multiple IDs are bound, separate them with "," commas.

(iv), other

Butter Knife provides a simplified code for Findviewbyid: FindByID, which can be used in view, activity, and dialog to find the desired view, and the generic type used by the method to convert the return value, that is, You can omit the Findviewbyid in front of the cast.

1 NULL ); 2 TextView firstName = Butterknife.findbyid (view, r.id.first_name);

The Butterknife.bind call can be placed anywhere you want to call Findviewbyid.

The Android butterknife annotation framework Uses

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.