这是转载别人的,写的比较好
Butter Knife Github地址: 点击打开链接
官方说明给出的解释是 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'
}
至此 配置完毕,具体使用就不详解了