Turn Android Libraries Introduction-Butter Knife

Source: Internet
Author: User
<span id="Label3"></p><p><p>Original address</p></p>Butter Knife-a Simple Introduction<p><p></p></p><p><p>Butter Knife-field and method binding for Android Views. Help you to simplify the code, easy to Read.</p></p>How to use<p><p>When you launch the Andriod app, there must be something like code:</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> class ExampleActivity extends Activity { TextView title; TextView subtitle; TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); title = (TextView)findViewById(R.id.title); subtitle = (TextView)findViewById(R.id.subtitle); footer = (TextView)findViewById(R.id.footer); //other codes }}</code></pre></pre><p><p>When you <code class=" language-markup">activity</code> have a lot of words, you will find that <code class=" language-markup">view</code> the code is very, very annoying and very annoying <code class=" language-markup">findViewById()</code> .</p></p><p><p>With the Butter kinfe, it can be simplified into</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> class ExampleActivity extends Activity { @Bind(R.id.title) TextView title; @Bind(R.id.subtitle) TextView subtitle; @Bind(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.simple_activity); ButterKnife.bind(this); // other codes }}</code></pre></pre><p><p>With <code class=" language-markup">@Bind</code> <code class=" language-markup">ButterKnife.bind()</code> The addition of a self-specified response <code class=" language-markup">view</code> . Is it a lot of simplicity?</p></p><p><p><code class=" language-markup">Fragment</code>You can also use:</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> public class FancyFragment 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.fancy_fragment, container, false); ButterKnife.bind(this, view); // TODO Use fields... return view; } }</code></pre></pre><p><p><code class=" language-markup">Adapter</code>No problem in the <code class=" language-markup">ViewHolder</code> following:</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> 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 = inflater.inflate(R.layout.whatever, parent, false); holder = new ViewHolder(view); view.setTag(holder); } holder.name.setText("John Doe"); // etc... return view; } static class ViewHolder { @Bind(R.id.title) TextView name; @Bind(R.id.job_title) TextView jobTitle; public ViewHolder(View view) { ButterKnife.bind(this, view); } } }</code></pre></pre><p><p>You can also <code class=" language-markup">TextView</code> put a few in <code class=" language-markup">List</code> the Middle:</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> @Bind({ R.id.first_name, R.id.middle_name, R.id.last_name }) List<span class="token tag"><span class="token tag"><span class="token punctuation"><EditText<span class="token punctuation">> nameViews;</span></span></span></span></code></pre></pre><p><p>can also be replaced by <code class=" language-markup">OnClickListener</code> :</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> @OnClick(R.id.submit) public void submit(View view) { // TODO submit data to server... }</code></pre></pre><p><p>A few of <code class=" language-markup">Button</code> you can use the same method.</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> @OnClick({ R.id.door1, R.id.door2, R.id.door3 }) public void submit(View view) { // TODO submit data to server... }</code></pre></pre>Installation method<p><p>Download the jar directly to the official website or use the Gradle</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> compile ‘com.jakewharton:butterknife:7.0.1‘ lintOptions { disable ‘InvalidPackage‘ }</code></pre></pre><p><p>Friends with IntelliJ idea may want to use Butter knife to Setting > Annotation processors.</p></p><p><p>If you use proguard, remember to add</p></p><pre class=" language-markup"><pre class=" language-markup"><code class=" language-markup"> -keep class butterknife.** { *; } -dontwarn butterknife.internal.** -keep class **$$ViewBinder { *; } -keepclasseswithmembernames class * { <span class="token tag"><span class="token tag"><span class="token punctuation"><fields<span class="token punctuation">>; } -keepclasseswithmembernames class * { @butterknife.* <span class="token tag"><span class="token tag"><span class="token punctuation"><methods<span class="token punctuation">>; }</span></span></span></span></span></span></span></span></code></pre></pre>Good<p><p>This is enough to make code Simple. ah, We're.</p></p>Summary<p><p>Butter knife seems to have little effect and does not actually use it. But it's simple and convenient to use it, and now it's used in every Project. It is the kind of libraries that "there is no problem, but a use is addiction".</p></p>Connect <ul> <ul> <li>Butter Knife</li> </ul> </ul><p><p>Turn Android Libraries Introduction-Butter Knife</p></p></span>
Related Article

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.