jar包下載:
http://download.csdn.net/detail/seven2729/9147765
點擊事件沒反應:
右鍵項目配置properties>>java compiler>>Annotation processing>>Factory Path,
勾選Enable project specific settings ,然后Add JARS,從項目libs文件夾下選擇依賴包饼酿,點擊完成堆缘。
-
在Activity里的寫法
// 綁定View對象 @Bind(R.id.title) TextView title; @Bind(R.id.subtitle) TextView subtitle; @Bind(R.id.footer) TextView footer; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // TODO 在這里直接用變量即可 }
在Fragment里的寫法:
注意最好在onDestoryView中進行解綁
/**
* Fragment生命周期比較特殊
* 需要在onDestroyView及時釋放這些View引用,避免造成內存溢出
*/
public class FancyFragment extends Fragment {
@Bind(R.id.button1)
Button button1;
@Bind(R.id.button2)
Button button2;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fancy_fragment, container, false);
ButterKnife.bind(this, view); // 進行綁定
// TODO Use fields...
return view;
}
@Override
public void onDestroyView() {
super.onDestroyView();
ButterKnife.unbind(this); // 解除綁定
}
}
- 在數據適配器的ViewHolder中的寫法:
@Bind(R.id.title) TextView name;
@Bind(R.id.job_title) TextView jobTitle;
public ViewHolder(View view) {
ButterKnife.bind(this, view); // 進行綁定, 將view中的指定id對當前對象進行注入
}
- 依賴注入的其他寫法:
// 也可以將這些View放到集合中
@Bind({ R.id.title, R.id.subtitle, R.id.footer })
List<TextView> textViews;
// 綁定字符串
@BindString(R.string.app_name) String titile;
// 綁定圖片
@BindDrawable(R.drawable.ic_launcher) Drawable drawable;
// 綁定顏色值, 也可以是顏色選擇器
@BindColor(R.color.colorPrimary) int colorPrimary; // int or ColorStateList field
// 綁定以像素為單位的尺寸或數值
@BindDimen(R.dimen.spacer) float spacer; // int (for pixel size) or float (for exact value) field
/**
* 給指定控件添加點擊事件, 注意:參數View可以省略, 或者寫成Button
* @param view
*/
@OnClick(R.id.bt_test)
public void submit(View view) {
// TODO submit data to server...
Toast.makeText(this, "single declare!", Toast.LENGTH_SHORT).show();
}
/**
* 給多個控件添加點擊事件, 注意:參數View可以省略, 或者寫成Button
* @param view
*/
@OnClick({R.id.bt_test, R.id.bt_test2})
public void submit2(View view) {
// TODO submit data to server...
switch (view.getId()) {
case R.id.bt_test:
Toast.makeText(this, "multi declare1!", Toast.LENGTH_SHORT).show();
break;
case R.id.bt_test2:
Toast.makeText(this, "multi declare2!", Toast.LENGTH_SHORT).show();
break;
}
}
/**
* 添加長按事件
* @return 表示是否消費長按事件 true消費
*/
@OnLongClick(R.id.bt_test) boolean sayGetOffMe() {
Toast.makeText(this, "Let go of me!", Toast.LENGTH_SHORT).show();
return true;
}
// 給ListView添加條目點擊監(jiān)聽
@OnItemSelected(R.id.list_view)
void onItemSelected(int position) {
// TODO ...
}
5娶靡、添加混淆忽略:
-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$ViewBinder { *; }
-keepclasseswithmembernames class * {
@butterknife.* <fields>;
}
-keepclasseswithmembernames class * {
@butterknife.* <methods>;
}