Android butterknife注解框架

欢迎大家访问我的博客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 下载地址:点击打开链接

阅读更多
个人分类: android
想对作者说点什么? 我来说一句

Android Butterknife框架Demo

2015年11月04日 1.28MB 下载

没有更多推荐了,返回首页

不良信息举报

Android butterknife注解框架

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭