Android Studio中ButterKnife的使用

71 篇文章 0 订阅
45 篇文章 0 订阅

1.Android Studio 安装ButterKnife插件

   同安装其他插件类似,如下:

   1.1 打开Plugins界面





按照上图中1,2,3指示操作(注意:这里我的Android Studio中已经安装了该插件,所以显示的内容不太一样)。然后重启Android Studio。


2.在项目上使用该开源项目(以Android Studio 为例)

  2.1 在bulid.gradle中添加依赖



重新编译一下该项目,通过后继续操作。


2.2 在代码中就可以使用注解的方式了

2.2.1 示例布局文件如下:

[html]  view plain  copy
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context=".MainActivity">  
  11.   
  12.     <TextView  
  13.         android:id="@+id/text_veiw_tv1"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:text="TextView 1" />  
  17.   
  18.     <Button  
  19.         android:id="@+id/button_bt1"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="Button1" />  
  23.   
  24.     <TextView  
  25.   
  26.         android:id="@+id/text_veiw_tv2"  
  27.         android:layout_width="match_parent"  
  28.         android:layout_height="wrap_content"  
  29.         android:text="TextView 2" />  
  30.   
  31.   
  32.     <Button  
  33.         android:id="@+id/button_bt2"  
  34.         android:layout_width="match_parent"  
  35.         android:layout_height="wrap_content"  
  36.         android:text="Button2" />  
  37.   
  38.   
  39. </LinearLayout>  



2.2.2 在代码中使用注解


选择上述布局文件名,右键







选择“Confirm”后,就会自动生成各个在布局文件中带有id 属性的view的注解形式

如下所示:

[html]  view plain  copy
  1. @Bind(R.id.text_veiw_tv1)  
  2. TextView textVeiwTv1;  
  3. @Bind(R.id.text_veiw_tv2)  
  4. TextView textVeiwTv2;  
  5. @Bind(R.id.button_bt1)  
  6. Button buttonBt1;  
  7. @Bind(R.id.button_bt2)  
  8. Button buttonBt2;  
  9.   
  10. @Override  
  11. protected void onCreate(Bundle savedInstanceState) {  
  12.     super.onCreate(savedInstanceState);  
  13.     setContentView(R.layout.activity_main);  
  14.     ButterKnife.bind(this);  
  15.   
  16.   
  17. }  

标注如下:



3 在代码中就可以点击事件

3.1 在代码里加入@OnClick(控件Id)

    @OnClick(R.id.button_bt1)
    public void test1(){
        Log.e(TAG, "onTest");
    }

3.2 gradle里添加“annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' 

dependencies {
    compile 'com.jakewharton:butterknife:8.4.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0'
}

当前gradle版本是2.2.0

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}




Android StudioButterKnife是一个用于简化Android开发视图绑定的开源库。它可以帮助开发者通过注解方式快速地绑定视图资源,减少findViewById的使用,提高开发效率。 使用ButterKnife,首先需要在项目的build.gradle文件添加依赖: ```groovy dependencies { implementation 'com.jakewharton:butterknife:10.2.1' annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.1' } ``` 然后,在需要使用ButterKnife的Activity或Fragment使用`@BindView`注解来绑定视图资源。 例如,在Activity绑定一个TextView: ```java public class MainActivity extends AppCompatActivity { @BindView(R.id.textView) TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // 视图绑定 // 使用textView textView.setText("Hello ButterKnife!"); } } ``` 在Fragment使用类似的方式绑定视图资源。 需要注意的是,使用ButterKnife进行视图绑定时,必须在`setContentView()`之后调用`ButterKnife.bind(this)`来完成绑定。 除了`@BindView`注解外,ButterKnife还提供了其他注解,如`@OnClick`用于点击事件的绑定、`@Nullable`用于可为空的注解等。 值得一提的是,自从Android Studio 3.6版本起,Google推出了ViewBinding功能,它提供了类似ButterKnife的视图绑定功能,并且是官方支持的。如果使用最新版本的Android Studio,建议使用ViewBinding来代替ButterKnife
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值