Android Butterknife view注入框架使用

前言
ButterKnife 简介

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

项目github地址:

https://github.com/JakeWharton/butterknife

ButterKnife 优势

1、强大的View绑定和Click事件处理功能,简化代码,提升开发效率
2、方便的处理Adapter里的ViewHolder绑定问题
3、运行时不会影响APP效率,使用配置方便
4、代码清晰,可读性强

如何添加依赖

在module的build.gredle 文件中的dependencies标签中添加

compile 'com.jakewharton:butterknife: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

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 ) ;

    }
}

zelezny : Butterknife插件的使用
可以在Androidstudio中安装zelezny插件可视化view注入。

你可能感兴趣的:(Android--基础)