"Butter knife" relies on injection to simplify code and improve development efficiency

Source: Internet
Author: User

Butter knife is a very good open source framework designed to streamline code and improve project development efficiency.

In the past we often need to use Findviewbyid (r.xx.xxx), almost no page will be involved, regardless of activity or fragment even the ListView, the GridView

Adapter. These repetitive codes can be tedious, because closing your eyes without knocking the wrong code, repeating hundreds of times a day is a bit time-consuming, despite the code hints. It is possible to nullpoint null pointers because of the forgotten writing of different field.



Then go directly to the topic:

Steps:

Third-party frameworks and libraries introduce the same laws, 1. Download the library, 2. Introduction, 3. Browse the document and view the API. There are some library projects, some jar packages, but basically the same, and finally pay attention to the problem of code confusion.

1, the preparation stage, first to the official website ( http://jakewharton.github.io/butterknife/ ) on the jar package, download down.

2, the download down the jar package, put in the project Libs, will automatically import the project.

3, the configuration eclips, the mouse alignment needs the annotation item, right click Poperties–>java compiler–>

Annotation procession–> hook to Enable project specific settings The others will automatically hook up.

–> Factory Path (Hook enable project specific settings) –> last add .... JARs the jar package that was just downloaded. This eclips configuration is possible.


4, the following is a picture explanation.


5, is to use annotations, directly on the code.

XML layout:

<relativelayoutxmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent ">     < TextView   android:id= "@+id/tv_test"   android:layout_width= "wrap_content"   android:layout_height= "wrap _content "   android:layout_centerhorizontal=" true "   android:layout_centervertical=" true "   Android:text = "@string/hello_world"   tools:context= ". Mainactivity "/></relativelayout>

Code section:

Import Butterknife. Butterknife;import Butterknife. Injectview;import Butterknife. Onclick;import android.os.bundle;import android.app.activity;import Android.widget.TextView;  public class Mainactivity extends Activity {@InjectView (r.id.tv_test) TextView tvtest; @Overridepublic void OnCreate ( Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Butterknife.inject (this); Tvtest.settext ("Test");} @OnClick (r.id.tv_test) public void Sayhi () {Tvtest.settext ("hello!");}}

Now: RUN it! It will be funny! look is not a bit of meaning, do not just look at the busy, do it, you can also like a master to write code, efficiency does not need their poor oh.


Reference: http://jakewharton.github.io/butterknife/Here are a variety of examples of using annotations, it is not specific to demonstrate, because you can see it again, beginners can also try oh.

In the confusion, don't forget to add the following code:

-dontwarn Butterknife.internal.**-keep class **$ $ViewInjector {*;} -keepnames class * {@butterknife. Injectview *;}




"Butter knife" relies on injection to simplify code and improve development efficiency

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.