Android Butterknife

导语

ButterKnife是一个专注于Android系统的View注入框架,可以减少大量的findViewById以及setOnClickListener代码,可视化一键生成。由大神JakeWharton开源。ButterKnife 地址

  • 依赖

  1. 在项目的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
}
  1. 在module的build.gredle 文件中添加
    apply plugin: 'android-apt'
  2. 在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'
}
ButterKnife 加库
  • 使用

  1. 控件id 注解:@BindView()
package com.zyj.wifi;
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 ");
        }
}
  1. 多个控件id 注解: @BindViews()
package com.zyj.wifi;
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 ");
        }
}
  1. fragment 使用
package com.zyj.wifi;
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 ;
        }
}
  1. @BindString() :绑定string 字符串
package com.zyj.wifi;
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 );
        }
}
  1. @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.zyj.wifi;
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] );
        }
}
  1. @BindBitmap( ) :绑定Bitmap 资源
package com.zyj.wifi;
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 );
        }
}
  1. @BindColor( ) : 绑定一个颜色值
package com.zyj.wifi;
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 );
        }
}
  1. @OnClick( ) : 绑定控件点击事件
  2. @OnLongClick( ) :绑定控件长按事件
package com.zyj.wifi;
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 Zelezny 安装
ButterKnife Zelezny 绑定范例

推荐阅读更多精彩内容