最近剛學會使用ButterKnife,真是超級好用,忍不住要分享給大家了。

寫在前面:該文件使用7.0版本,8.0版本方法名有所改動,建議看官方文件,整體業務邏輯和原理沒什麼變動。

在android程式設計過程中,我們會寫大量的佈局和點選事件,像初始view、設定view監聽這樣簡單而重複的操作讓人覺得麻煩類,所以可以採用註解的方式去實現,而ButterKnife則是註解中相對簡單易懂的很不錯的開源框架,而網上的文件和例子都過時了,7.0之後的版本改動很大,之前的註解都不能用了,所以借鑑官方文件總結了一下,接下來就介紹一下如何使用。基本參照官方文件,加上自己的心得。

ButterKnife 優勢:

1.強大的View繫結和Click事件處理功能,簡化程式碼,提升開發效率

2.方便的處理Adapter裡的ViewHolder繫結問題

3.執行時不會影響APP效率,使用配置方便

4.程式碼清晰,可讀性強

使用心得:

1.Activity ButterKnife.bind(this);必須在setContentView();之後,且父類bind繫結後,子類不需要再bind

2.Fragment ButterKnife.bind(this, mRootView);

3.屬性佈局不能用private or static 修飾,否則會報錯

4.setContentView()不能通過註解實現。(其他的有些註解框架可以)

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

使用步驟:

一.匯入ButterKnife jar包:

1)如果你是Eclipse,可以去官網下載jar包
2)如果你是AndroidStudio可以直接 File->Project Structure->Dependencies->Library dependency 搜尋butterknife即可,第一個就是
3)當然也可以用maven和gradle配置

<span style="font-family:SimSun;font-size:14px;">MAVEN  
    <dependency>  
      <groupId>com.jakewharton</groupId>  
      <artifactId>butterknife</artifactId>  
      <version>(insert latest version)</version>  
    </dependency>  
  
GRADLE  
compile 'com.jakewharton:butterknife:(insert latest version)'  
  
Be sure to suppress this lint warning in your build.gradle.(關閉)  
lintOptions {  
  disable 'InvalidPackage'  
}  </span>

注意如果在Library 專案中使用要按如下步驟(github中有具體描述)否則無法找到view:


注:官網github也有對應的引用步驟。

二.常見使用方法:
1)由於每次都要在Activity中的onCreate繫結Activity,所以個人建議寫一個BaseActivity完成繫結,子類繼承即可
     注:ButterKnife.bind(this);繫結Activity 必須在setContentView之後:
     實現如下(FragmentActivity 實現一樣):

<span style="font-family:SimSun;font-size:14px;">public abstract class BaseActivity extends Activity {  
    public abstract int getContentViewId();  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(getContentViewId());  
        ButterKnife.bind(this);  
        initAllMembersView(savedInstanceState);  
    }  
  
    protected abstract void initAllMembersView(Bundle savedInstanceState);  
  
    @Override  
    protected void onDestroy() {  
        super.onDestroy();  
        ButterKnife.unbind(this);//解除繫結,官方文件只對fragment做了解綁  
    }  
}  </span>


2)繫結fragment
<span style="font-family:SimSun;font-size:14px;">public abstract class BaseFragment extends Fragment {  
    public abstract int getContentViewId();  
    protected Context context;  
    protected View mRootView;  
  
    @Nullable  
    @Override  
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {  
        mRootView =inflater.inflate(getContentViewId(),container,false);  
        ButterKnife.bind(this,mRootView);//繫結framgent  
        this.context = getActivity();  
        initAllMembersView(savedInstanceState);  
        return mRootView;  
    }  
  
    protected abstract void initAllMembersView(Bundle savedInstanceState);  
  
    @Override  
    public void onDestroyView() {  
        super.onDestroyView();  
        ButterKnife.unbind(this);//解綁  
    }  
}  </span>

3)繫結view

<span style="font-family:SimSun;font-size:14px;">@Bind(R.id.hello_world)  
TextView mHelloWorldTextView;  
@Bind(R.id.app_name)  
TextView mAppNameTextView;//view</span>

4)繫結資源
<span style="font-family:SimSun;font-size:14px;">@BindString(R.string.app_name)  
String appName;//sting  
@BindColor(R.color.red)  
int textColor;//顏色  
@BindDrawable(R.mipmap.ic_launcher)  
Drawable drawable;//drawble  
@Bind(R.id.imageview)  
ImageView mImageView;  
@Bind(R.id.checkbox)  
CheckBox mCheckBox;  
@BindDrawable(R.drawable.selector_image)  
Drawable selector; </span>

5)Adapter ViewHolder 繫結

<span style="font-family:SimSun;font-size:14px;">public class TestAdapter extends BaseAdapter {  
    private List<String> list;  
    private Context context;  
  
    public TestAdapter(Context context, List<String> list) {  
        this.list = list;  
        this.context = context;  
    }  
  
    @Override  
    public int getCount() {  
        return list==null ? 0 : list.size();  
    }  
  
    @Override  
    public Object getItem(int position) {  
        return list.get(position);  
    }  
  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
  
    @Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
        ViewHolder holder;  
        if (convertView == null) {  
            convertView = LayoutInflater.from(context).inflate(R.layout.layout_list_item, null);  
            holder = new ViewHolder(convertView);  
            convertView.setTag(holder);  
        } else {  
            holder = (ViewHolder) convertView.getTag();  
        }  
        holder.textview.setText("item=====" + position);  
        return convertView;  
    }  
  
    static class ViewHolder {  
        @Bind(R.id.hello_world)  
        TextView textview;  
  
        public ViewHolder(View view) {  
            ButterKnife.bind(this, view);  
        }  
    }  
}  </span>

6)點選事件的繫結:不用宣告view,不用setOnClickLisener()就可以繫結點選事件

a.直接繫結一個方法

<span style="font-family:SimSun;font-size:14px;">@OnClick(R.id.submit)  
public void submit(View view) {  
  // TODO submit data to server...  
} </span>

b.所有監聽方法的引數是可選的

<span style="font-family:SimSun;font-size:14px;">@OnClick(R.id.submit)  
public void submit() {  
  // TODO submit data to server...  
}  </span>

c.定義一個特定型別,它將自動被轉換

<span style="font-family:SimSun;font-size:14px;">@OnClick(R.id.submit)  
public void sayHi(Button button) {  
  button.setText("Hello!");  
}</span>

d.多個view統一處理同一個點選事件,很方便,避免抽方法重複呼叫的麻煩

<span style="font-family:SimSun;font-size:14px;">@OnClick({ R.id.door1, R.id.door2, R.id.door3 })  
public void pickDoor(DoorView door) {  
  if (door.hasPrizeBehind()) {  
    Toast.makeText(this, "You win!", LENGTH_SHORT).show();  
  } else {  
    Toast.makeText(this, "Try again", LENGTH_SHORT).show();  
  }  
} </span>

 e.自定義view可以繫結自己的監聽,不指定id

<span style="font-family:SimSun;font-size:14px;">public class FancyButton extends Button {  
  @OnClick  
  public void onClick() {  
    // TODO do something!  
  }  
}  </span>

f.給EditText加addTextChangedListener(即新增多回調方法的監聽的使用方法),利用指定回撥,實現想回調的方法即可,哪個註解不會用點進去看下原始碼上的註釋就會用了

<span style="font-family:SimSun;font-size:14px;">@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.BEFORE_TEXT_CHANGED)  
void beforeTextChanged(CharSequence s, int start, int count, int after) {  
  
}  
@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.TEXT_CHANGED)  
void onTextChanged(CharSequence s, int start, int before, int count) {  
  
}  
@OnTextChanged(value = R.id.mobileEditText, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)  
void afterTextChanged(Editable s) {  
  
}  </span>

7)對一組View進行統一操作

a.裝入一個list

<span style="font-family:SimSun;font-size:14px;">@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name })  
List<EditText> nameViews; </span>

b.設定統一處理

<span style="font-family:SimSun;">static final ButterKnife.Action<View> DISABLE = new ButterKnife.Action<View>() {  
  @Override public void apply(View view, int index) {  
    view.setEnabled(false);  
  }  
};  
static final ButterKnife.Setter<View, Boolean> ENABLED = new ButterKnife.Setter<View, Boolean>() {  
  @Override public void set(View view, Boolean value, int index) {  
    view.setEnabled(value);  
  }  
};<span style="color: silver; line-height: normal; background-color: rgb(248, 248, 248);"> </span><a target=_blank href="http://blog.csdn.net/itjianghuxiaoxiong/article/details/50177549#" class="ViewSource" title="view plain" style="line-height: normal; color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_plain.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: 0% 0%; background-repeat: no-repeat;">view plain</a></span><span data-mod="popu_168" style="font-size: 9px; font-family: Verdana, Geneva, Arial, Helvetica, sans-serif; color: silver; line-height: normal;"> <a target=_blank href="http://blog.csdn.net/itjianghuxiaoxiong/article/details/50177549#" class="CopyToClipboard" title="copy" style="color: rgb(160, 160, 160); text-decoration: none; border: none; padding: 1px; margin: 0px 10px 0px 0px; font-size: 9px; display: inline-block; width: 16px; height: 16px; text-indent: -2000px; background-image: url("images/default/ico_copy.gif"); background-attachment: initial; background-color: inherit; background-size: initial; background-origin: initial; background-clip: initial; background-position: left top; background-repeat: no-repeat;">c</a></span>

c.統一操作處理,例如設定是否可點,屬性等

<span style="font-family:SimSun;">ButterKnife.apply(nameViews, DISABLE);  
ButterKnife.apply(nameViews, ENABLED, false);</span><span style="font-family:Arial;font-size: 14px;"> </span>
8)可選繫結:預設情況下,“繫結”和“監聽”繫結都是必需的。如果不能找到目標檢視,則將丟擲異常。所以做空處理
<span style="font-family:SimSun;">@Nullable @Bind(R.id.might_not_be_there) TextView mightNotBeThere;  
  
@Nullable @OnClick(R.id.maybe_missing) void onMaybeMissingClicked() {  
  // TODO ...  
}  </span>

三、程式碼混淆
<span style="font-family:SimSun;font-size:14px;">-keep class butterknife.** { *; }  
-dontwarn butterknife.internal.**  
-keep class **$$ViewBinder { *; }  
  
-keepclasseswithmembernames class * {  
    @butterknife.* <fields>;  
}  
  
-keepclasseswithmembernames class * {  
    @butterknife.* <methods>;  
}  </span>

四、Zelezny外掛的使用

在AndroidStudio->File->Settings->Plugins->搜尋Zelezny下載新增就行 ,可以快速生成對應元件的例項物件,不用手動寫。使用時,在要匯入註解的Activity 或 Fragment 或 ViewHolder的layout資原始碼上,右鍵——>Generate——Generate ButterKnife Injections,然後就出現如圖的選擇框。(此動態圖來自官網)


原文連結:點選開啟連結