不会偷懒的程序员不是一个好的程序员。为了发扬一下懒人精神,今天就推荐一款集偷懒与简约于一身的开发库Butterknife。在刚开始用的时候确实挺麻烦的,有些包明明导入成功了,可是代码上就是没有自动提示出来,不过经过查阅,终于找到解决方法了,所以记录在此,下次导入错误就再回来看就行了。
一、在工程的根目录上的build.gradle文件中,添加这么一行代码
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.0.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' //看到没有,就是这么一句
// 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
}
二、在module的build.gradle中添加几行代码,然后同步一下就可以啦
apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'//第一句
android {
compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
applicationId "com.binge.myapplication"
minSdkVersion 8
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'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.1.1'
compile 'com.android.support:design:24.1.1'
compile 'com.jakewharton:butterknife:8.1.0'//第二句
apt 'com.jakewharton:butterknife-compiler:8.1.0'//第三句
}
三、butterknife使用
1.在使用butterknife中的快捷方式之前呢,记得要在Activity的setContentView后面添加这么一句
ButterKnife.bind(this);
2.我自己比较常用的就是@BindView和@onClick两个注解了
@BindView(R.id.,main_tv)
TextView tv;//声明成功后,以后直接调用tv就行了,跟普通的findViewById之后再使用是一个样的
@onClick(R.id.main_bt)//这个就可将点击事件直接写在send方法里面了,就不用再去findViewById,然后再去setOnclicklistener,轻轻松松搞定
void send(){
}