本文出自:http://blog.youkuaiyun.com/u013647382/article/details/52002840
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, 找到插件点击安装. 我下面的截图是安装之后的, 大家都懂, 不用多解释.重启生效
步骤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 24
buildToolsVersion "24.0.0"
defaultConfig {
applicationId "org.yxm.butterknifesimple"
minSdkVersion 15
targetSdkVersion 24
versionCode 1
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);
, 否则不起作用
使用教程
使用很简单, 大家可自行参照官方教程, 就一页而已, 看看就明白了.
官方地址:
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