关闭

Android butterknife注解框架

293人阅读 评论(0) 收藏 举报
分类:

欢迎大家访问我的博客http://blog.csdn.net/mikejaps,专注于android ios  app 开发


首先提下ButterKnifey已经更新到版本7.0.1了,现在注解已经不叫@InjectView了,而叫@Bind,感觉更贴合语义。同时注册的方式也从

ButterKnife.inject(this);

变成了
ButterKnife.bind(this);

关于7.0.1的ButterKnife的各种用法和功能,可以参考ButterKnife的github上的主页:

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

//activity中使用

public class MainActivity extends Activity {
 
  @InjectView(R.id.tv_test)
  TextView tvTest;
  @OnClick(R.id.submit)
  public void sayHi(Button button) {
    button.setText("Hello!");
  }																   																   @Override
  public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main);
     ButterKnife.inject(this);
     tvTest.setText("test");
  }
 
  @OnClick(R.id.tv_test)
  public void sayHi() {
     tvTest.setText("Hello!");
  }
}
// fragment中使用
public class FancyFragment extends Fragment {

@InjectView(R.id.button1) Button button1;

@InjectView(R.id.button2) Button button2;

 

  @Override View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fancy_fragment, container, false);

    ButterKnife.inject(this, view);

   // TODO Use "injected" views...

    return view;

 }

 

 @Override void onDestroyView() {

   super.onDestroyView();

   ButterKnife.reset(this);

  }

}
//adpter中使用

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 convertView;

}

 
static class ViewHolder {

    @InjectView(R.id.title) TextView name;

    @InjectView(R.id.job_title) TextView jobTitle;

 

    public ViewHolder(View view) {

       ButterKnife.inject(this, view);

    }

  }

}

butterknife.jar 下载地址:点击打开链接

1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:28345次
    • 积分:828
    • 等级:
    • 排名:千里之外
    • 原创:50篇
    • 转载:28篇
    • 译文:2篇
    • 评论:6条
    文章分类
    最新评论