Android常用之Butterknife使用详解
使用Butterknife很久了,一直没有写过文章,今天记录一下
在Android 主Moduel中的build.gradle中添加如下配置:
dependencies {
implementation 'com.jakewharton:butterknife:8.8.1'
annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
}
复制代码
使用如下
/**
* des:我的主页
* Created by xsf
* on 2016.09.17:07
*/
public class CareMainFragment extends BaseFragment<MyMainPresenter, MyMainModel>
implements MyMainContract.View {
@Bind(R.id.img_logo)
ImageView imgLogo;
@Bind(R.id.tv_username)
EditText tvUsername;
@Bind(R.id.root_view)
LinearLayout rootLayout;
@Bind(R.id.tv_times)
TextView tvTimes;
@Bind(R.id.irc_my)
IRecyclerView myIrc;
@Bind(R.id.ll_apply)
LinearLayout mApplyLayout;
@Bind(R.id.tv_apply)
TextView mApplyTv;
@Bind(R.id.tv_transform)
TextView mTvTransform;
@Bind(R.id.img_left)
ImageView mImageLeft;
@Bind(R.id.img_one)
ImageView mImageOne;
@Bind(R.id.img_two)
ImageView mImageTwo;
@Bind(R.id.img_three)
ImageView mImageThree;
@Bind(R.id.img_right)
ImageView mImageRight;
MyCenterAdapter myCenterAdapter;
private boolean isShowTopic;
private boolean isShowGroup;
private int shareNum;
private String headUri, shareImg;
private String token;
private ProgressDialog progressDialog;
private Uri uritempFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.butter)
public void onClick(){
Log.e("111111","11111");
Toast.makeText(this, "绑定单个view事件", Toast.LENGTH_SHORT).show();
}
复制代码
打开源码Bind,写的很清楚:将字段绑定到指定ID的视图。该视图将自动投射到该字段
/**
* Bind a field to the view for the specified ID. The view will automatically be cast to the field
* type.
* <pre><code>
* {@literal @}Bind(R.id.title) TextView title;
* </code></pre>
*/
@Retention(CLASS) @Target(FIELD)
public @interface Bind {
/** View ID to which the field will be bound. */
int[] value();
}
复制代码
image
更多详解:
喜欢可以加@群号:913934649
简书: https://www.jianshu.com/u/88db5f15770d
csdn:https://me.csdn.net/beyondforme
掘金:https://juejin.im/user/5e09a9e86fb9a016271294a7
————————————————
版权声明:本文为CSDN博主「踏云归」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/beyondforme/article/details/103917234
网友评论