Android @BindView的基本使用

在app的gradle中导包

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test.ext:junit:1.1.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
   
    //BindView导包
    implementation 'com.jakewharton:butterknife:10.1.0'//加这个
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'//加这个

同时注意加1.8

compileOptions {

        sourceCompatibility JavaVersion.VERSION_1_8

        targetCompatibility JavaVersion.VERSION_1_8

    }

在Activity中的使用

  @BindView(R.id.tv_number)
     TextView tv_number;
     
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);//这段代码一定要加上不然会导致程序启动失败内存溢出,程序崩溃

绑定注解

@BindView---->绑定一个view;id为一个view 变量
@BindView(R.id.tv_fm1)
TextView tvFm1;

@BindViews ---->绑定多个view;id为一个view的list变量
@BindViews({ R.id.btn1,R.id.btn2 })
List buttons;

@BindArray---->绑定string里面array数组;
@BindArray(R.array.city )
String[] citys ;

@BindBitmap---->绑定图片资源为Bitmap;
@BindBitmap(R.mipmap.wifi )
Bitmap bitmap;

@BindBool ---->绑定boolean值
@BindColor ---->绑定color;
@BindColor(R.color.colorAccent)
int black;

@BindDimen ---->绑定Dimen;
@BindDimen(R.dimen.borth_width)
int mBorderWidth;

@BindDrawable ----> 绑定Drawable;
@BindDrawable(R.drawable.test_pic)
Drawable mTestPic;

@BindFloat ---->绑定float
@BindInt ---->绑定int
@BindString ---->绑定一个String id为一个String变量;
@BindString(R.string.app_name )
String meg;

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值