Butter Knife 配置及使用入門詳解
簡介
Butter Knife 是一個工具, 可以通過注解(Annotation)的形式綁定Android中的各種資源, 例如: 控件, string, color, bitmap 等, 減少像 findViewById(), setOnClickListener()等重複代碼, 讓代碼更加簡潔, 同時也提高了程式員的開發效率.
配置
配置Butter Knife 隻需要3個步驟:
1. 安裝AS插件 Android ButterKnife Zelezny
2. 在項目中添加項目依賴
3. 開始使用注解
步驟1
首先打開AS, 打開 Settings -> Plugins -> 搜尋Android ButterKnife Zelezny, 找到插件點選安裝. 我下面的截圖是安裝之後的, 大家都懂, 不用多解釋. 重新開機生效
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM3EjMxYTN5AzMycDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
步驟2
建立Android項目, 分别找到Project Gradle配置檔案和Modle Gradle配置檔案.
在Project Gradle 配置檔案中添加:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // 添加這行
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
再在 Module Gradle配置檔案中添加:
apply plugin: 'com.android.application'
android {
compileSdkVersion
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "org.yxm.butterknifesimple"
minSdkVersion
targetSdkVersion
versionCode
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
apply plugin: 'com.neenbedankt.android-apt' //添加這行
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.jakewharton:butterknife:8.2.1' //添加這行
apt 'com.jakewharton:butterknife-compiler:8.2.1' //添加這行
}
然後點選 Sync Now, 更新配置, 稍等片刻等待完成
步驟3
開始使用
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
<TextView
android:id="@+id/tv_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<Button
android:id="@+id/btn_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@BindView(R.id.tv_1)
TextView tv1;
@BindView(R.id.btn_1)
Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.tv_1)
void tv1OnClick(View view) {
String content = ((TextView) view).getText().toString();
Toast.makeText(MainActivity.this, "" + content, Toast.LENGTH_SHORT).show();
}
@OnClick(R.id.btn_1)
void btnOnClick() {
Toast.makeText(MainActivity.this, "test", Toast.LENGTH_SHORT).show();
}
}
@BindView(R.id.tv_1)
綁定控件
@OnClick(R.id.tv_1)
綁定事件
注意: 一定記住在 onCreate() 中調用 ButterKnife.bind(this);
, 否則不起作用
ButterKnife.bind(this);
使用教程
使用很簡單, 大家可自行參照官方教程, 就一頁而已, 看看就明白了.
官方位址:
http://jakewharton.github.io/butterknife/
主要支援
AS輸入 @bind 出現代碼提示:
AS輸入 @on 出現代碼提示:
我們可以看到大多數的資源和監聽事件我們都可以通過注解來綁定. 當然還有很多其他的, 大家自己查閱
ButterKnife.bind(…)的使用
如果在Activity綁定 ButterKnife 使用:
@Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_activity);
ButterKnife.bind(this);
// TODO Use fields...
}
在Fragment中綁定 ButterKnife使用:
@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;
}
在 BaseAdapter的 ViewHodler中使用:
static class ViewHolder {
@BindView(R.id.title) TextView name;
@BindView(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
還有很多進階用法, 大家看看上面的官方位址:
http://jakewharton.github.io/butterknife/