隐藏

Android Studio3.5.2使用@BindView代替findViewById

发布:2023/2/25 10:02:50作者:管理员 来源:本站 浏览次数:422

使用findViewById太繁琐了,@BindView能够很好的减少一些不必要的工作。


1、在build.grable(Moudle:app)中加入


   implementation 'com.jakewharton:butterknife:10.0.0'

   annotationProcessor 'com.jakewharton:butterknife-compiler:10.0.0'


2、找到File->Settings->Pligins->搜索框输入ButterKnife->安装Android ButterKnife Zelezny


3、xml文件如下:


   <?xml version="1.0" encoding="utf-8"?>

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

       xmlns:app="http://schemas.android.com/apk/res-auto"

       android:layout_width="match_parent"

       android:layout_height="match_parent"

       android:gravity="center">

   

   

       <Button

           android:id="@+id/button"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:text="Button" />

   </LinearLayout>


4、 光标移到布局文件上,我这里是activity_main,右键单击 -> Generate -> Generate ButterKnife Injections->Confirm




5、结果如下,成功了。