轉載請注明出處:http://www.cnblogs.com/cnwutianhao/p/6610529.html
Butter Knife
Github地址: https://github.com/JakeWharton/butterknife
官方說明給出的解釋是 Bind Android views and callbacks to fields and methods.
Field and method binding for Android views which uses annotation processing to generate boilerplate code for you.
- Eliminate
findViewById
calls by using@BindView
on fields. - Group multiple views in a list or array. Operate on all of them at once with actions, setters, or properties.
- Eliminate anonymous inner-classes for listeners by annotating methods with
@OnClick
and others. - Eliminate resource lookups by using resource annotations on fields.
意思就是:將Android視圖和回調綁定到字段和方法。使用注釋處理為您生成樣板代碼的Android視圖的字段和方法綁定。通過在字段上使用@BindView消除findViewById調用。在列表或數組中分組多個視圖。 使用操作,設置器或屬性一次操作所有這些。通過使用@OnClick和其他方法注釋方法,消除匿名內部類的偵聽器。通過在字段上使用資源注釋來消除資源查找。
官方文檔給出了Butter Knife在Gradle里面如何配置
下面我們就來配置Buffer Knife到工程里面
新建Android Studio工程(作者的Android Studio版本是2.3)
項目主體結構如下:
首先,配置build.gradle(Project:XXX)
新建工程的build.gradle(Project:XXX)初始樣子如下:
然后我們按照官方文檔進行配置
在repositories {}里面添加
mavenCentral()
在dependencies {}里面添加
classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'
build.gradle(Project:XXX)完整代碼即:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0' classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
其次,修改build.gradle(Module:XXX)
新建工程的build.gradle(Module:XXX)初始樣子如下:
然后我們按照官方文檔進行配置
在開頭添加
apply plugin: 'com.jakewharton.butterknife'
在dependencies {} 里面添加
compile 'com.jakewharton:butterknife:8.5.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'
build.gradle(Module:XXX)完整代碼即:
apply plugin: 'com.android.application'
apply plugin: 'com.jakewharton.butterknife' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.tnnowu.android.butterknifedemo" minSdkVersion 14 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.0' compile 'com.android.support.constraint:constraint-layout:1.0.2' testCompile 'junit:junit:4.12' compile 'com.jakewharton:butterknife:8.5.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1' }
至此,我們的Butter Knife就已經正確的配置到我們的工程里面了。
彩蛋:
做過安卓項目的同學都知道,比如工程里面的get、set 方法,都是通過Generate快速編譯的。(說到這里如果你沒看懂的話,說明你的Android開發經驗太少了,請自行百度查找)
又有同學問了,我在Github上面看到大神們用Butter Knife去編寫代碼,初始化的時候總是蒙圈。其實 Butter Knife的初衷就是簡化代碼,讓程序員更有效率的開發。
這時候可以利用Android Studio里面的 Plugins來輔助我們開發。
File -> Settings -> Plguins
然后在右面的搜索框里面輸入 ButterKnife,然后點擊下面的 Search in repositories
就會出現下圖的場景:
選擇第一個 Android ButterKnife PPlugin Plus 然后點擊 Install,重啟Android Studio
重啟之后,Butter Knife就可以很好的為我們效勞了。
那么Butter Knife如何使用呢?
首先,打開布局文件
Android Studio更新到2.3以后的布局文件初始是這樣的
為了做演示,我們稍微修改一下,添加幾個控件。
一個EditText,兩個Button
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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" android:orientation="vertical" tools:context="com.tnnowu.android.butterknifedemo.MainActivity"> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="50dp" /> <Button android:id="@+id/btn1" android:layout_width="match_parent" android:layout_height="50dp" /> <Button android:id="@+id/btn2" android:layout_width="match_parent" android:layout_height="50dp" /> </LinearLayout>
然后,我到Class類里面去用 Butter Knife綁定控件,代替 findViewById
初始Class類文件如下:
在布局文件上面右鍵,選擇Generate
然后選擇 Generate Butterknife Injections
這里我們可以看到布局文件中的控件都出現在上面,可以額外選擇點擊事件OnClick
Confirm之后的場景如下:
package com.tnnowu.android.butterknifedemo;
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.EditText; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @BindView(R.id.edit_text) EditText editText; @BindView(R.id.btn1) Button btn1; @BindView(R.id.btn2) Button btn2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick({R.id.btn1, R.id.btn2}) public void onClick(View view) { switch (view.getId()) { case R.id.btn1: break; case R.id.btn2: break; } } }
可以清晰的看到 Butter Knife 已經開始為我們效勞了。
(注意:ButterKnife.bind(this) 是一定要寫在onCreate里面)
學會使用 BUffer Knife,可以讓程序自動化幫忙減輕工作量!
關注我的新浪微博,獲取更多Android開發資訊!
關注科技評論家,領略科技、創新、教育以及最大化人類智慧與想象力!