ButterKnife簡單使用和注意事項

版權聲明:本文爲博主原創文章,未經博主允許不得轉載。 https://blog.csdn.net/JourneyX/article/details/73647859
1、ButterKnife使用

網上最新的依賴是8…的,如果添加不了依賴就添加如下版本較低的6…..
添加對應的工具+compile'com.jakewharton:butterknife:6.1.0'
compile 'com.jakewharton:butterknife:8.1.0'
Android Studio中添加:ButterKnifeZelezny插件。
初始化:ButterKnife.inject(this);

1、下載插件:



2、添加依賴

     以一個簡單的項目作爲實例:
dependencies {
    compile fileTree(dir'libs'include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group'com.android.support'module'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:'+gradle.ext.compileSupportversion
    testCompile 'junit:junit:4.12'

//    compile 'com.jakewharton:butterknife:8.1.0'

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

}

簡單佈局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xys.butterknifetest.MainActivity">

    <TextView
        android:id="@+id/tv_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Hello World!"/>

    <ImageView
        android:id="@+id/img_show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/tv_show"
        android:layout_marginTop="20dp"
        android:background="@mipmap/ic_launcher"/>

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/img_show"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="事件1"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="事件2"/>


    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="事件3"/>




</RelativeLayout>


3、實例代碼和操作演示:
     首先完成初始化操作:
ButterKnife.bind(this);

-------》快速生成應用資源代碼:
setContentView(R.layout.activity_main);
光標選中佈局文件,右擊:


下一步:

快速可視化界面如下:

功能性實例代碼:

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class MainActivity extends Activity {


    @BindView(R.id.tv_show)
    TextView mTvShow;
    @BindView(R.id.img_show)
    ImageView mImgShow;
    @BindView(R.id.btn1)
    Button mBtn1;
    @BindView(R.id.btn2)
    Button mBtn2;
    @BindView(R.id.btn3)
    Button mBtn3;
    @BindView(R.id.activity_main)
    RelativeLayout mActivityMain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();

    }

    @OnClick(R.id.btn1)
    public void onMBtn1Clicked() {
        mTvShow.setText("事件1觸發");
        Toast.makeText(this, "事件1觸發"Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.btn2)
    public void onMBtn2Clicked() {
        mTvShow.setText("事件2觸發");
        Toast.makeText(this, "事件2觸發"Toast.LENGTH_SHORT).show();
    }

    @OnClick(R.id.btn3)
    public void onMBtn3Clicked() {
        mTvShow.setText("事件3觸發");
        Toast.makeText(this, "事件3觸發"Toast.LENGTH_SHORT).show();
    }
}

4、理論說明
===關於ButterKnife服務註冊:
     在Activity上使用 ButterKnife.bind(this)
     在非Activity上使用ButterKnife.bind(this,view)

===綁定View控件:
/**單個View控件的綁定*/
@BindView(R.id.btn_login)
/**多個控件的綁定可以寫在List或者Array中*/
@BindViews({ R.id.first_name, R.id.middle_name, R.id.last_name })
List<EditText> nameViews;

===資源綁定
@BindString(R.string.title) String title;
@BindDrawable(R.drawable.graphic) Drawable graphic;
@BindColor(R.color.red) int red; // int or ColorStateList field
@BindDimen(R.dimen.spacer) Float spacer; // int (for pixel size) or float (for exact value) field

===監聽器綁定
@OnClick(R.id.btn3)
public void onMBtn3Clicked() {
    mTvShow.setText("事件3觸發");
    Toast.makeText(this, "事件3觸發"Toast.LENGTH_SHORT).show();
}
--多個控件可以綁定同一個監聽器
@OnClick(R.id.btn3,R.id.btn5)
public void onMBtn3Clicked() {
    mTvShow.setText("事件3觸發");
    Toast.makeText(this, "事件3觸發"Toast.LENGTH_SHORT).show();
}

===Fragment重置Binding
Fragment的生命週期不同於Activity,當Butterknife在onCreateView上進行綁定時,需要再onDestroyView上進行解綁,Butterknife.bind()方法提供了一個Unbinder 返回值,在onDestroyView上調用相關的unbinder方法即可:
public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1; 
@BindView(R.id.button2) Button button2;
private Unbinder unbinder; 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    View view = inflater.inflate(R.layout.fancy_fragment, container,false); 
    unbinder = ButterKnife.bind(this, view); 
// TODO Use fields... return view; 

@Override public void onDestroyView() 
    super.onDestroyView(); 
    unbinder.unbind(); 
}}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章