# ButterKnife **Repository Path**: HackerX9/ButterKnife ## Basic Information - **Project Name**: ButterKnife - **Description**: BaseActivity BaseFragment - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2017-11-06 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README 编译的时候,ButterKnifeProcessor类的process()方法便会执行,搜索到所有的ButterKnife注解,然后生成一个 Java 类。执行 ButterKnife.bind(this) 时,ButterKnife会加载上面生成的类,然后调用其bind方法。这里首先调用了findRequiredView去寻找R.id.text1所对应的控件,其实相当于我们的findViewById();其次调用castView相当于类型转换,把找到的View转化为TextView类型 # ButterKnife ## 添加依赖 1. 在项目的project 的build.gredle 文件中的dependencies标签下添加--->classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' ``` buildscript { repositories { google() jcenter() } dependencies { classpath 'com.android.tools.build:gradle:3.0.0-beta2' // ButterKnife classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // ButterKnife } } allprojects { repositories { google() jcenter() } } task clean(type: Delete) { delete rootProject.buildDir } ``` 2. 在module的build.gredle 文件中添加--->apply plugin: 'android-apt' 3. 在module的build.gredle 文件中的dependencies标签中添加--->compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' ``` apply plugin: 'com.android.application' // ButterKnife apply plugin: 'android-apt' // ButterKnife android { compileSdkVersion 26 buildToolsVersion "26.0.0" defaultConfig { applicationId "com.palfund.butterknife" minSdkVersion 19 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.0.1' implementation 'com.android.support.constraint:constraint-layout:1.0.2' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.0' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.0' // ButterKnife compile 'com.jakewharton:butterknife:8.4.0' apt 'com.jakewharton:butterknife-compiler:8.4.0' // ButterKnife } ``` ## Activity中绑定 ``` @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 绑定Activity ButterKnife.bind(this); } ``` ## Fragment中绑定 ``` Unbinder unbinder; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_blank, container, false); // 绑定Fragment unbinder = ButterKnife.bind(this, view); return view; } @Override public void onDestroyView() { super.onDestroyView(); // 解除绑定 unbinder.unbind(); } ``` ### 用法 1. 绑定View ``` @BindView(R.id.btn1) Button mBtn1; ``` 2. 绑定多个View ``` @BindViews({R.id.tv1, R.id.tv2, R.id.tv3}) List list_textView; ``` 3. View的点击 ``` @OnClick(R.id.btn1) public void onViewClicked() { } // 监听器方法的参数是可选的 @OnClick(R.id.btn1) public void onViewClicked(View view) { } // 指定一个特定的类型,Butter Knife也能识别 @OnClick(R.id.btn1) public void onViewClicked(Button btn) { } ``` 4. Views的点击 ``` @OnClick({R.id.tv2, R.id.tv3}) public void onViewClicked(View view) { switch (view.getId()) { case R.id.tv2: break; case R.id.tv3: break; } } ``` 5. 绑定string ``` @BindString(R.string.app_name) String bindString; ``` 6. 绑定数组 ``` @BindArray(R.array.bindArray) String []bindArray; ``` 7. 绑定Bitmap ``` @BindBitmap(R.mipmap.ic_launcher) Bitmap mBitmap; ``` 8. 绑定Color ``` @BindColor(R.color.bindColor) int bindColor; ``` 9. 绑定Drawable ``` @BindDrawable(R.drawable.ic_launcher) Drawable mDrawable; ``` 10. 该函数一次性在列表中的所有View上执行一个动作 ``` ButterKnife.apply(list_textView, new ButterKnife.Action() { @Override public void apply(@NonNull TextView view, int index) { view.setEnabled(false); } }); // Android中的Property属性也可以使用apply方法进行设置 ButterKnife.apply(list_textView, View.ALPHA, 0.5f); ```