Android注解利器:ButterKnife 的基本使用
ch1990
8年前
<h2>前言</h2> <ul> <li> <p>ButterKnife 简介</p> <p>ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,可视化一键生成。</p> </li> </ul> <ul> <li> <p>ButterKnife 优势</p> <p>1、强大的View绑定和Click事件处理功能,简化代码,提升开发效率</p> <p>2、方便的处理Adapter里的ViewHolder绑定问题</p> <p>3、运行时不会影响APP效率,使用配置方便</p> <p>4、代码清晰,可读性强</p> </li> </ul> <h2>如何添加依赖</h2> <ul> <li> <p>在项目的project 的build.gredle 文件中的dependencies标签下添加。</p> <p>classpath 'com.neenbedankt.gradle.plugins:android- apt:1.8 '</p> </li> </ul> <p>例如:</p> <pre> <code class="language-java">buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.2' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } </code></pre> <ul> <li> <p>在module的build.gredle 文件中添加</p> <p>apply plugin: 'android-apt'</p> </li> <li> <p>在module的build.gredle 文件中的dependencies标签中添加</p> <p>compile 'com.jakewharton:butterknife:8.4.0'</p> <p>apt 'com.jakewharton:butterknife-compiler:8.4.0'</p> </li> </ul> <p>例如</p> <pre> <code class="language-java">apply plugin: 'com.android.application' apply plugin: 'android-apt' android { compileSdkVersion 24 buildToolsVersion "24.0.3" defaultConfig { applicationId "com.zyj.wifi" minSdkVersion 14 targetSdkVersion 24 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' } </code></pre> <h2>如何使用</h2> <ul> <li>控件id 注解: @BindView()</li> </ul> <pre> <code class="language-java">package com.dl.ButterKnifeDemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) public Button button1 ; // 注意:button 的修饰类型不能是:private 或者 static 。 否则会报错:错误: @BindView fields must not be private or static. (com.zyj.wifi.ButterknifeActivity.button1) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; button1.setText( "I am a button "); } } </code></pre> <ul> <li>多个控件id 注解: @BindViews()</li> </ul> <pre> <code class="language-java">package com.dl.ButterKnifeDemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import java.util.List; import butterknife.BindViews; import butterknife.ButterKnife; public class Main2Activity extends AppCompatActivity { @BindViews({ R.id.button1 , R.id.button2 , R.id.button3 }) public List<Button> buttonList ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ButterKnife.bind(this); buttonList.get( 0 ).setText( "hello 1 "); buttonList.get( 1 ).setText( "hello 2 "); buttonList.get( 2 ).setText( "hello 3 "); } } </code></pre> <ul> <li>fragment 使用</li> </ul> <pre> <code class="language-java">package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeFragment extends Fragment { @BindView( R.id.button1 ) public Button button1 ; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_butterknife, container, false); //绑定fragment ButterKnife.bind( this , view ) ; button1.setText( "I am a button "); return view ; } } </code></pre> <ul> <li>@BindString() :绑定string 字符串</li> </ul> <pre> <code class="language-java">package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import butterknife.BindString; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定button 控件 public Button button1 ; @BindString( R.string.app_name ) //绑定string 字符串 String meg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; button1.setText( meg ); } } </code></pre> <ul> <li>@BindArray() : 绑定string里面array数组</li> </ul> <pre> <code class="language-java"><resources> <string name="app_name">WiFi管家</string> <string-array name="city"> <item>厦门市</item> <item>福州市</item> <item>泉州市</item> <item>漳州市</item> <item>龙岩市</item> </string-array> </resources> package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import butterknife.BindArray; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定button 控件 public Button button1 ; @BindArray(R.array.city ) //绑定string里面array数组 String [] citys ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; button1.setText( citys[0] ); } } </code></pre> <ul> <li>@BindBitmap( ) : 绑定Bitmap 资源</li> </ul> <pre> <code class="language-java">package com.dl.ButterKnifeDemo; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import butterknife.BindBitmap; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.imageView ) //绑定ImageView 控件 public ImageView imageView ; @BindBitmap( R.mipmap.wifi ) //绑定Bitmap 资源 public Bitmap wifi_bitmap ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; imageView.setImageBitmap( wifi_bitmap ); } } </code></pre> <ul> <li>@BindColor( ) : 绑定一个颜色值</li> </ul> <pre> <code class="language-java">package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import butterknife.BindColor; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定一个控件 public Button button1 ; @BindColor( R.color.colorAccent ) int black ; //绑定一个颜色值 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; button1.setTextColor( black ); } } </code></pre> <ul> <li>@OnClick( ) : 绑定控件点击事件</li> <li>@OnLongClick( ) : 绑定控件长按事件</li> </ul> <pre> <code class="language-java">package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.OnLongClick; public class ButterknifeActivity extends AppCompatActivity { @OnClick(R.id.button1 ) //给 button1 设置一个点击事件 public void showToast(){ Toast.makeText(this, "is a click", Toast.LENGTH_SHORT).show(); } @OnLongClick( R.id.button1 ) //给 button1 设置一个长按事件 public boolean showToast2(){ Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show(); return true ; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; } } </code></pre> <ul> <li> <p>zelezny : Butterknife插件的使用</p> <p>插件的安装</p> </li> </ul> <p style="text-align:center"><img src="https://simg.open-open.com/show/cdc6bd7fa7de7f6d3a761d41fccf1d70.png"></p> <p style="text-align:center"><img src="https://simg.open-open.com/show/180d36b1714a0ace65e28d1fae6ce453.png"></p> <p>插件的使用</p> <p>安装完成插件后,会提示重启AS,重启完后,可以写一个布局并且新建一个代码类测试下。测试的过程中要注意的是,需要 将光标移到setContentView(R.layout.acty_login) ,将光标放到R.layout.acty_login,然后右键Generate就有了。要注意一定要将光标放在R.layout.acty_login上面。</p> <p style="text-align:center"><img src="https://simg.open-open.com/show/84b66a1c0caee9c008da2a266095c9a7.gif"></p> <p>这里需要注意的是在勾选控件的界面上,有一个CreateViewHolder , 很明显这个是专门为ListView或者RecyclerView的适配器专门提供的。</p> <p> </p> <p>来自:http://www.cnblogs.com/mysticCoder/p/6266544.html</p> <p> </p>