首页 > 代码库 > Android注解利器:ButterKnife 的基本使用
Android注解利器:ButterKnife 的基本使用
前言
-
ButterKnife 简介
ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,可视化一键生成。
项目github地址:https://github.com/JakeWharton/butterknife
-
ButterKnife 优势
1、强大的View绑定和Click事件处理功能,简化代码,提升开发效率
2、方便的处理Adapter里的ViewHolder绑定问题
3、运行时不会影响APP效率,使用配置方便
4、代码清晰,可读性强
如何添加依赖
-
在项目的project 的build.gredle 文件中的dependencies标签下添加。
classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘
例如:
buildscript { repositories { jcenter() } dependencies { classpath ‘com.android.tools.build:gradle:2.2.2‘ classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘ } } allprojects { repositories { jcenter() } } task clean(type: Delete) { delete rootProject.buildDir }
-
在module的build.gredle 文件中添加
apply plugin: ‘android-apt‘
-
在module的build.gredle 文件中的dependencies标签中添加
compile ‘com.jakewharton:butterknife:8.4.0‘
apt ‘com.jakewharton:butterknife-compiler:8.4.0‘
例如
apply plugin: ‘com.android.application‘ apply plugin: ‘android-apt‘ android { compileSdkVersion 24 buildToolsVersion "24.0.3" defaultConfig { applicationId "com.zyj.wifi" minSdkVersion 14 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‘]) compile ‘com.jakewharton:butterknife:8.4.0‘ apt ‘com.jakewharton:butterknife-compiler:8.4.0‘ }
如何使用
- 控件id 注解: @BindView()
package com.dl.ButterKnifeDemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) public Button button1 ; // 注意:button 的修饰类型不能是:private 或者 static 。 否则会报错:错误: @BindView fields must not be private or static. (com.zyj.wifi.ButterknifeActivity.button1) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; button1.setText( "I am a button "); } }
- 多个控件id 注解: @BindViews()
package com.dl.ButterKnifeDemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import java.util.List; import butterknife.BindViews; import butterknife.ButterKnife; public class Main2Activity extends AppCompatActivity { @BindViews({ R.id.button1 , R.id.button2 , R.id.button3 }) public List<Button> buttonList ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); ButterKnife.bind(this); buttonList.get( 0 ).setText( "hello 1 "); buttonList.get( 1 ).setText( "hello 2 "); buttonList.get( 2 ).setText( "hello 3 "); } }
- fragment 使用
package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeFragment extends Fragment { @BindView( R.id.button1 ) public Button button1 ; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_butterknife, container, false); //绑定fragment ButterKnife.bind( this , view ) ; button1.setText( "I am a button "); return view ; } }
- @BindString() :绑定string 字符串
package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import butterknife.BindString; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定button 控件 public Button button1 ; @BindString( R.string.app_name ) //绑定string 字符串 String meg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; button1.setText( meg ); } }
- @BindArray() : 绑定string里面array数组
<resources> <string name="app_name">WiFi管家</string> <string-array name="city"> <item>厦门市</item> <item>福州市</item> <item>泉州市</item> <item>漳州市</item> <item>龙岩市</item> </string-array> </resources> package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import butterknife.BindArray; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定button 控件 public Button button1 ; @BindArray(R.array.city ) //绑定string里面array数组 String [] citys ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; button1.setText( citys[0] ); } }
- @BindBitmap( ) : 绑定Bitmap 资源
package com.dl.ButterKnifeDemo; import android.graphics.Bitmap; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.ImageView; import butterknife.BindBitmap; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.imageView ) //绑定ImageView 控件 public ImageView imageView ; @BindBitmap( R.mipmap.wifi ) //绑定Bitmap 资源 public Bitmap wifi_bitmap ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; imageView.setImageBitmap( wifi_bitmap ); } }
- @BindColor( ) : 绑定一个颜色值
package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; import butterknife.BindColor; import butterknife.BindView; import butterknife.ButterKnife; public class ButterknifeActivity extends AppCompatActivity { @BindView( R.id.button1 ) //绑定一个控件 public Button button1 ; @BindColor( R.color.colorAccent ) int black ; //绑定一个颜色值 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; button1.setTextColor( black ); } }
- @OnClick( ) : 绑定控件点击事件
- @OnLongClick( ) : 绑定控件长按事件
package com.dl.ButterKnifeDemo; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Toast; import butterknife.ButterKnife; import butterknife.OnClick; import butterknife.OnLongClick; public class ButterknifeActivity extends AppCompatActivity { @OnClick(R.id.button1 ) //给 button1 设置一个点击事件 public void showToast(){ Toast.makeText(this, "is a click", Toast.LENGTH_SHORT).show(); } @OnLongClick( R.id.button1 ) //给 button1 设置一个长按事件 public boolean showToast2(){ Toast.makeText(this, "is a long click", Toast.LENGTH_SHORT).show(); return true ; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_butterknife); //绑定activity ButterKnife.bind( this ) ; } }
-
zelezny : Butterknife插件的使用
插件的安装
插件的使用
安装完成插件后,会提示重启AS,重启完后,可以写一个布局并且新建一个代码类测试下。测试的过程中要注意的是,需要
将光标移到setContentView(R.layout.acty_login)
,将光标放到R.layout.acty_login,然后右键Generate就有了。要注意一定要将光标放在R.layout.acty_login上面。
这里需要注意的是在勾选控件的界面上,有一个CreateViewHolder , 很明显这个是专门为ListView或者RecyclerView的适配器专门提供的。
|
Android注解利器:ButterKnife 的基本使用