ButterKnife

classpath 'com.jakewharton:butterknife-gradle-plugin:8.4.0'
在project里面配置

build.gradle    module里面配置以下两个
apply plugin: 'com.jakewharton.butterknife'




compile 'com.jakewharton:butterknife:8.4.0'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'




public class MainActivity extends AppCompatActivity {
    String url = "http://ww3.sinaimg.cn/large/610dc034jw1f6m4aj83g9j20zk1hcww3.jpg";
  @BindView(R.id.sd)
    SimpleDraweeView sd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
        //这个绑定方法必须在setcontentview之后,不然会报错
        ButterKnife.bind(this);
           sd.setImageURI(Uri.parse(url));

    }
    @OnClick(R.id.sd)
 public void dianji(View view){
     Toast.makeText(MainActivity.this,"你猜",Toast.LENGTH_SHORT).show();
 }

 

 
 
例如在Fragment中:


public class FancyFragment extends Fragment {
@BindView(R.id.button1) Button button1;
@BindView(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;
}
}
还有一种比较常见的场景,就是在ListView的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 {
@BindView(R.id.title)
TextView name;
@BindView(R.id.job_title) TextView jobTitle;

public ViewHolder(View view) {
ButterKnife.bind(this, view);
}
}
}



参考
https://jingyan.baidu.com/album/48b37f8d37ca921a64648833.html?picindex=7   里面有配置图
https://note.youdao.com/share/?id=8e4e2b78b084ffe08769bfc02ca83745&type=note#/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值