1. 程式人生 > >ButterKnife 專門為Android view繫結註解,專業解決各種findViewById

ButterKnife 專門為Android view繫結註解,專業解決各種findViewById

Butter Knife,專門為Android View設計的繫結註解的依賴:

           compile 'com.jakewharton:butterknife:8.8.1'
           annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'    

  • 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.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 ");
    }
}
  • 多個控制元件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 ");
    }
}

  • 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 ;
    }
}
  • @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 );
    }
}

  • @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] );
    }
}

  • @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 );
    }
}
  • @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 );

    }
}

  • @OnClick( ) : 繫結控制元件點選事件
  • @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 ) ;

    }
}