ButterKnife简单使用教程(Eclipse版)
写在前面:该文档使用7.0.1版本
jar下载:http://download.csdn.net/detail/u012771445/9555428
源码下载:
ButterKnife是Android视图字段和方法绑定,使用注解处理的方式来生成样板代码。
主要特性是:
- 使用@BindView字段消除findViewById调用
- 多个view统一处理同一个事件
- 通过@OnClick注释方法消除匿名内部类与各种监听器
- 利用资源注释方法查找资源
1.使用步骤
Eclipse使用配置:
在Package Explorer中找到你的Project右击,定位到Properties → Java Compiler → Annotation Processing,勾选”Enable project specific settings”,最终效果如下图所示:
展开Annotation Processing选项卡,选择Factory Path,勾选Enable project specific settings,然后点击”Add JARs…”,定位到Butter Knife jar,然后选择它,最终效果如下图所示:
点击”Ok”按钮来保存你的设置,接下来会弹出对话框”rebuild your project”,你需要点击”Yes”
clean你的project
- 配置完成,但是要确认你的Java版本最低是1.6。
2.常见的使用方法
绑定View
class ExampleActivity extends Activity { @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.simple_activity); ButterKnife.bind(this); // TODO Use fields... } }
绑定Fragment
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; } }
绑定资源
@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;
Adapter ViewHolder绑定
public class MyAdapter extends BaseAdapter { @Override public View getView(int position, View view, ViewGroup parent) { ViewHolder holder; if (view != null) { holder = (ViewHolder) view.getTag(); } else { view = inflater.inflate(R.layout.whatever, parent, false); holder = new ViewHolder(view); view.setTag(holder); } holder.name.setText("John Doe"); // etc... return view; } static class ViewHolder { @Bind(R.id.title) TextView name; @Bind(R.id.job_title) TextView jobTitle; public ViewHolder(View view) { ButterKnife.bind(this, view); } } }
点击事件的绑定
@OnClick(R.id.submit) public void submit(View view) { // TODO submit data to server... }
多个view统一处理同一个点击事件,很方便,避免抽方法重复调用的麻烦
@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(); } }
自定义view可以绑定自己的监听,不指定id
public class FancyButton extends Button { @OnClick public void onClick() { // TODO do something! } }
对一组View进行统一操作
@Bind({ R.id.first_name, R.id.middle_name, R.id.last_name }) List<EditText> nameViews;
绑定重置
Fragment与Activity生命周期不同。fragment在onCreateView中绑定,需要在onDestroyView中设置为null,ButterKnife有一个方法自动这样做。
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); } }
官方Github介绍:https://github.com/JakeWharton/butterknife