一、概念
- ButterKnife是一个专注于Android系统的View注入框架。
- 使用ButterKnife对性能基本没有损失,因为ButterKnife用到的注解并不是在运行时使用反射,而是在编译时生成新的类。
- 项目集成以及使用都特别简单。
二、使用
//build.gradle(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
//build.gradle(module)
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.0"
defaultConfig {
applicationId "com.example.sourcecodetest"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
// Butterknife requires Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.jakewharton:butterknife:10.2.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1'
}
//strings.xml
<resources>
<string name="app_name">SourceCodeTest</string>
<string-array name="city">
<item>北京</item>
<item>上海</item>
<item>广州</item>
</string-array>
</resources>
//colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#008577</color>
<color name="colorPrimaryDark">#00574B</color>
<color name="colorAccent">#D81B60</color>
</resources>
//activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"/>
<Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn1"
android:text="Button2"/>
<Button
android:id="@+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn2"
android:text="Button3"/>
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/btn3"
android:text="TextView1"/>
</RelativeLayout>
//MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
@BindViews({ R.id.btn1, R.id.btn2, R.id.btn3})
public List<Button> buttonList ;
@BindView(R.id.tv1)
public TextView textView;
@BindString(R.string.app_name)
String str;
@BindArray(R.array.city)
String[] arr;
@BindColor(R.color.colorAccent)
int color;
@BindBitmap(R.drawable.ic_launcher)
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "zwm, onCreate");
ButterKnife.bind(this);
buttonList.get(0).setText("ButterKnife Button1");
buttonList.get(1).setText("ButterKnife Button2");
buttonList.get(2).setText("ButterKnife Button3");
textView.setText("ButterKnife TextView1");
Log.d(TAG, "zwm, str: " + str);
if(arr != null && arr.length > 0) {
for(String item : arr) {
Log.d(TAG, "zwm, item: " + item);
}
}
textView.setBackgroundColor(color);
Log.d(TAG, "zwm ,bitmap: " + bitmap);
}
@OnClick(R.id.btn1)
public void showShortClickToast() {
Toast.makeText(this, "This is a short click", Toast.LENGTH_SHORT).show();
}
@OnLongClick(R.id.btn1)
public void showLongClickToast() {
Toast.makeText(this, "This is a long click", Toast.LENGTH_SHORT).show();
}
@OnClick({R.id.btn2, R.id.btn3})
public void onViewClick(View view) {
switch (view.getId()) {
case R.id.btn2:
Toast.makeText(this, "This is a btn2 click", Toast.LENGTH_SHORT).show();
break;
case R.id.btn3:
Toast.makeText(this, "This is a btn3 click", Toast.LENGTH_SHORT).show();
break;
}
}
}
//输出log
2020-01-13 11:44:39.543 12202-12202/com.example.sourcecodetest D/MyApplication: zwm, onCreate
2020-01-13 11:44:41.331 12202-12202/com.example.sourcecodetest D/MainActivity: zwm, onCreate
2020-01-13 11:44:41.336 12202-12202/com.example.sourcecodetest D/MainActivity: zwm, str: SourceCodeTest
2020-01-13 11:44:41.337 12202-12202/com.example.sourcecodetest D/MainActivity: zwm, item: 北京
2020-01-13 11:44:41.337 12202-12202/com.example.sourcecodetest D/MainActivity: zwm, item: 上海
2020-01-13 11:44:41.337 12202-12202/com.example.sourcecodetest D/MainActivity: zwm, item: 广州
2020-01-13 11:44:41.337 12202-12202/com.example.sourcecodetest D/MainActivity: zwm ,bitmap: android.graphics.Bitmap@a645210
三、源码解析
ButterKnife源码解析
网友评论