《Android进阶之光》--ButterKnife

本文详细介绍了ButterKnife库的使用方法,包括依赖配置、视图绑定、资源绑定及多种事件监听绑定方式,并深入解析了ButterKnife注解处理器的工作流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

No1:

添加依赖库

Project的build.gradle文件添加

buildscript{
    ...
    dependencies{
    ...
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

在Module:app的build.gradle文件添加

apply plugin:'com.neenbedankt.android-apt'
...
dependencies{
...
compile 'com.jakewharton:butterknife:8.4.0'
apt 'com.jakewharton:butterknife-compiler:8.4.0'
}

No2:

绑定控件id

@BindView(R.id.tv_text)
TextView tv_text;

No3:

绑定资源

@BindString(R.string.app_name)
String appName;
@BindArray(R.array.swordsman)
String[] swordsman;
@BindDimen(R.dimen.activity_horizontal_margin)
float margin;

No4:

绑定监听

@OnClick(R.id.bt_button1)
public void showToast(){
}

@OnLongClick(R.id.bt_button2)
public boolean setText(Button button){
    return true;
}
@OnTextChanged(value=R.id.et_edittext,callback=OnTextChanged.Callback.BEFORE_TEXT_CHANGED)
void beforeTextChanged(CharSequence s,int start,int count,int after){
}

@OnTextChanged(value=R.id.et_edittext,callback=OnTextChanged.Callback.TEXT_CHANGED)
void onTextChanged(CharSequence s,int start,int before,int count){
}

@OnTextChanged(value=R.id.et_edittext,callback=OnTextChanged.Callback.AFTER_TEXT_CHANGED)
void afterTextChanged(Editable s){
}
@OnTouch(R.id.bt_button3)
public boolean onTouch(View view,MotionEvent event){
    return true;
}

@OnItemClick(R.id.lv_list)
void onItemClick(int position){
}

No5:

可选绑定

@Nullable
@BindView(R.id.tv_toptext)
TextView name;

No6:

ButterKnife注解处理器是ButterKnifeProcessor,主要处理逻辑都在process方法中

->findAndParseTargets

->brewJava

->createBinding

->findBindingConstructorForClass

->findRequiredViewAsType

->findRequiredView

->DecorView.findViewById

->castView

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值