ButterKnife 8.5.1使用總結

   最近在做一個OA項目,其中有許多的界面都是表單類型的數據收集,沒完沒了的findViewById敲的我手都麻木了,於是找到ButterKnife框架。實踐過後覺得確實好用,個人認爲代碼結構也更清晰易讀了,使用總結如下。


ButterKnife 簡介

   ButterKnife是一個專注於Android系統的View注入框架,可以減少大量的findViewById以及setOnClickListener代碼。在7.0版本以後引入了註解處理器,取代了之前利用反射原理進行findViewById影響APP性能的實現方式,不再影響APP運行效率。

 

GitHub: https://github.com/JakeWharton/butterknife

官方網站: http://jakewharton.github.io/butterknife/

 

使用須知:

1.Activity中使用ButterKnife.bind(this);必須在setContentView();之後,父類中bind後,子類不需要再bind

2.Fragment中使用ButterKnife.bind(this, rootView); 根佈局View對象,需要在onDestroyView中解綁

3.註解的屬性不能用private或static修飾,否則會報錯

4.Activity官方例子中沒有解綁操作,而Fragment需要解綁

 

使用步驟:

1、添加依賴

    compile 'com.jakewharton:butterknife:8.5.1'

    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

備註:只需要添加上述依賴即可使用。

只有當你在Library中使用ButterKnife時,才需要如下步驟:

1、在Project的build.gradle中添加:

classpath'com.jakewharton:butterknife-gradle-plugin:8.5.1'

2、在對應Library(Module)中添加:

apply plugin:'com.jakewharton.butterknife'

網上許多的使用文章都把上述兩個步驟當做是必須的。非也!

(注意:To use ButterKnife in a library

 

實際項目

實際項目中一般都會有定義一個BaseActivity或BaseFragment,在基類中綁定,則在其所有的子類中都可以直接使用。需要注意的是,父類中需要在子類的setContentView調用完後再綁定。

 

使用完整範例

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.textview)
    TextView textview;
    @BindView(R.id.button)
    Button button;

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

    @OnClick({R.id.textview, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.textview:
                break;
            case R.id.button:
                break;
        }
    }
}

@BindView(R.id.textview)//聲明並綁定變量
TextView textview;
//相當於:TextViewtextView = (TextView) findViewById(R.id.textView);
 
@OnClick(R.id.button)//給按鈕綁定方法,可以不聲明引用變量
public void onViewClicked() {
}
 
@BindViews({R.id.button1, R.id.button2, R.id.button3})
public List<Button> mList ;
 
@BindString(R.string.app_name) //綁定字符串
String str;
 
@BindArray(R.array.city ) //綁定數組
String[] arr ;

 

在fragment中使用(需要解綁)

private Unbinder unbinder;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_regist_create, container, false);
    unbinder = ButterKnife.bind(this, view);//需要加多一個加載了佈局的View對象
    return view;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    unbinder.unbind();//fragment需要解綁
}

 

再偷懶一點

Android Studio插件:Android Butterknife Zelezny

方便給整個佈局文件一鍵生成註解。

使用方法:

    在setContentView(R.layout.activity_main)中對着activity_main右鍵,generate,Generate ButterKnife Injections,勾選需要生成註解的控件即可

zelezny_animated.gif

圖片來自android-butterknife-zelezny


總結

一般我認爲簡化了findViewById和setOnClickListener之後,已經很方便了。並不會去使用ButterKnife中方方面面的功能,在多控件的界面例如提交表單數據時非常好用。這也就是“80/20”法則在生效吧!對於使用比較少的其他特性如解除綁定等,用到之時再嘗試即可!

 

參考文檔

[Android開發] ButterKnife8.5.1 使用方法教程總結

 

同類型框架參考

google/dagger

https://github.com/google/dagger

androidannotations

https://github.com/androidannotations/androidannotations

 

附錄:

註解類型

* @BindViews

* @BindView

* @BindArray

* @BindBitmap

* @BindBool

* @BindColor

* @BindDimen

* @BindDrawable

* @BindFloat

* @BindInt

* @BindString

 

事件類型

* @OnClick

* @OnCheckedChanged

* @OnEditorAction

* @OnFocusChange

* @OnItemClick item

* @OnItemLongClick

* @OnItemSelected

* @OnLongClick

* @OnPageChange

* @OnTextChanged

* @OnTouch

* @Optional

 

===================================================================================

Github上的使用說明:

Download

dependencies {

  compile 'com.jakewharton:butterknife:8.5.1'

  annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

}

Snapshots of thedevelopment version are available in Sonatype's snapshots repository.


Library projects

To use ButterKnife in a library, add the plugin to your buildscript:

buildscript {

  repositories {

    mavenCentral()

   }

  dependencies {

    classpath 'com.jakewharton:butterknife-gradle-plugin:8.5.1'

  }

}

and then apply it in your module:

    apply plugin: 'com.android.library'

    apply plugin: 'com.jakewharton.butterknife'

Now make sureyou use R2 instead of R inside allButter Knife annotations.

classExampleActivityextendsActivity {

    @BindView(R2.id.user) EditText username;

    @BindView(R2.id.pass) EditText password;

...

}

==================================================================================

 


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