Android常用之Butterknife使用详解

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();
}
复制代码

 

 

 

更多详解:
喜欢可以加@群号:913934649

简书: https://www.jianshu.com/u/88db5f15770d

csdn:https://me.youkuaiyun.com/beyondforme

掘金:https://juejin.im/user/5e09a9e86fb9a016271294a7
————————————————
版权声明:本文为优快云博主「踏云归」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/beyondforme/article/details/103895229

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值