Andriod Studio 生成@Bind即ButterKnife插件的安装与使用

1》Android Studio 安装ButterKnife插件

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

1.1》打开Plugins界面





按照上图中1,2,3指示操作(注意:这里我的Android Studio中已经安装了该插件,所以显示的内容不太一样,如果没有安装图3的应该是Install plugin,而不是Uninstall plugin,需要点击图3进行重启Android Studio)。


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

2.1》在bulid.gradle(Module:app)中添加依赖

compile 'com.jakewharton:butterknife:7.0.1'



然后点击Sync Project with Gradle Files重新编译一下该项目,通过后继续操作。


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. }  

标注如下:



  • 6
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值