findviewbyid的插件 --------ButterKnife



你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件
此文是介绍在eclipse环境下使用ButterKnife的,相比传统的findViewById确实简单了点~但是笔者认为还不够简单~~
在谷歌停止对ADT+Eclipse停止更新之后~笔者还苦苦在Eclipse坚持了几个月终于开始转移到Android Studio上撸代码~~
因此本文也是在Android Studio基础上写的,如果有用Eclipse可以参考:
你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件


ButterKnife简介



前面的文章已经介绍过了这里不介绍了!!!


ButterKnife+Android ButterKnife Zelezny组合

目前ButterKnife的最新版本是7.0.1
首先在项目的build.gradle文件中添加一句话:

[java]  view plain  copy
  1. compile 'com.jakewharton:butterknife:7.0.1'  
然后点击右上角的sync now,android studio就会自动下载ButterKnife
要想达到一键绑定控件的效果还需要安装Android ButterKnife Zelezny插件,
打开Android Studio设置面板--->Plugins---->可以看到有个搜索框我们输入ButterKnife----->然后点击下面的Browse Repositories---->选择Android ButterKnife Zelezny---->安装--->重启Android Studio----->完成~

好了到此为止就完成了。
下面我们来具体看看ButterKnife有多方便:
为了演示绑定控件,笔者写了几个控件:
[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4.     android:paddingRight="@dimen/activity_horizontal_margin"  
  5.     android:paddingTop="@dimen/activity_vertical_margin"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.   
  8.  <ImageView  
  9.      android:id="@+id/testImageId"  
  10.      android:layout_width="wrap_content"  
  11.      android:layout_height="wrap_content" />  
  12.     <TextView  
  13.         android:id="@+id/testTextId"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" />  
  16.     <Button  
  17.         android:id="@+id/testBtnId"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content" />  
  20.     <EditText  
  21.         android:id="@+id/testetId"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content" />  
  24.   
  25. </RelativeLayout>  


按照传统做法我们是在Activity上一个个find出来,就只有这几个控件还好,但是我们写程序的时候有些界面远远不止这几个控件~~难道我们就一个个地find出来吗?

看过布局文件我们回到Activity.java
首先在setContentView(XXX)的括号内容右击:


选择Generate:

选择Generate ButterKnife Injections:

 
到了这个界面可以看到列出了当前布局文件可以被find出来的控件,如果不需要再.java文件使用的控件可以不勾上,默认情况下控件命名与布局里的id名称一致。
我们再看左下角有一个Create ViewHolder,可以知道,ButterKnife也可以用在创建ViewHolder上,这里不再详述,接下来我们点击Confirm
转载:

记得之前写过一篇博客叫做:





你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件
此文是介绍在eclipse环境下使用ButterKnife的,相比传统的findViewById确实简单了点~但是笔者认为还不够简单~~
在谷歌停止对ADT+Eclipse停止更新之后~笔者还苦苦在Eclipse坚持了几个月终于开始转移到Android Studio上撸代码~~
因此本文也是在Android Studio基础上写的,如果有用Eclipse可以参考:
你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件


ButterKnife简介



前面的文章已经介绍过了这里不介绍了!!!

ButterKnife+Android ButterKnife Zelezny组合

目前ButterKnife的最新版本是7.0.1
首先在项目的build.gradle文件中添加一句话:
[java]  view plain  copy
  1. compile 'com.jakewharton:butterknife:7.0.1'  
然后点击右上角的sync now,android studio就会自动下载ButterKnife
要想达到一键绑定控件的效果还需要安装Android ButterKnife Zelezny插件,
打开Android Studio设置面板--->Plugins---->可以看到有个搜索框我们输入ButterKnife----->然后点击下面的Browse Repositories---->选择Android ButterKnife Zelezny---->安装--->重启Android Studio----->完成~
好了到此为止就完成了。
下面我们来具体看看ButterKnife有多方便:
为了演示绑定控件,笔者写了几个控件:
[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4.     android:paddingRight="@dimen/activity_horizontal_margin"  
  5.     android:paddingTop="@dimen/activity_vertical_margin"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.   
  8.  <ImageView  
  9.      android:id="@+id/testImageId"  
  10.      android:layout_width="wrap_content"  
  11.      android:layout_height="wrap_content" />  
  12.     <TextView  
  13.         android:id="@+id/testTextId"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" />  
  16.     <Button  
  17.         android:id="@+id/testBtnId"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content" />  
  20.     <EditText  
  21.         android:id="@+id/testetId"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content" />  
  24.   
  25. </RelativeLayout>  
按照传统做法我们是在Activity上一个个find出来,就只有这几个控件还好,但是我们写程序的时候有些界面远远不止这几个控件~~难道我们就一个个地find出来吗?
看过布局文件我们回到Activity.java
首先在setContentView(XXX)的括号内容右击:
选择Generate:
选择Generate ButterKnife Injections:
 
 

到了这个界面可以看到列出了当前布局文件可以被find出来的控件,如果不需要再.java文件使用的控件可以不勾上,默认情况下控件命名与布局里的id名称一致。
我们再看左下角有一个Create ViewHolder,可以知道,ButterKnife也可以用在创建ViewHolder上,这里不再详述,接下来我们点击Confirm




转载:http://blog.csdn.net/javy_codercoder/article/details/49511007

记得之前写过一篇博客叫做:





你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件
此文是介绍在eclipse环境下使用ButterKnife的,相比传统的findViewById确实简单了点~但是笔者认为还不够简单~~
在谷歌停止对ADT+Eclipse停止更新之后~笔者还苦苦在Eclipse坚持了几个月终于开始转移到Android Studio上撸代码~~
因此本文也是在Android Studio基础上写的,如果有用Eclipse可以参考:
你还在苦逼地findViewById吗?使用ButterKnife从此轻松定义控件


ButterKnife简介



前面的文章已经介绍过了这里不介绍了!!!


ButterKnife+Android ButterKnife Zelezny组合

目前ButterKnife的最新版本是7.0.1
首先在项目的build.gradle文件中添加一句话:

[java]  view plain  copy
  1. compile 'com.jakewharton:butterknife:7.0.1'  
然后点击右上角的sync now,android studio就会自动下载ButterKnife
要想达到一键绑定控件的效果还需要安装Android ButterKnife Zelezny插件,
打开Android Studio设置面板--->Plugins---->可以看到有个搜索框我们输入ButterKnife----->然后点击下面的Browse Repositories---->选择Android ButterKnife Zelezny---->安装--->重启Android Studio----->完成~

好了到此为止就完成了。
下面我们来具体看看ButterKnife有多方便:
为了演示绑定控件,笔者写了几个控件:
[html]  view plain  copy
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3.     android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4.     android:paddingRight="@dimen/activity_horizontal_margin"  
  5.     android:paddingTop="@dimen/activity_vertical_margin"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.   
  8.  <ImageView  
  9.      android:id="@+id/testImageId"  
  10.      android:layout_width="wrap_content"  
  11.      android:layout_height="wrap_content" />  
  12.     <TextView  
  13.         android:id="@+id/testTextId"  
  14.         android:layout_width="wrap_content"  
  15.         android:layout_height="wrap_content" />  
  16.     <Button  
  17.         android:id="@+id/testBtnId"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="wrap_content" />  
  20.     <EditText  
  21.         android:id="@+id/testetId"  
  22.         android:layout_width="wrap_content"  
  23.         android:layout_height="wrap_content" />  
  24.   
  25. </RelativeLayout>  


按照传统做法我们是在Activity上一个个find出来,就只有这几个控件还好,但是我们写程序的时候有些界面远远不止这几个控件~~难道我们就一个个地find出来吗?

看过布局文件我们回到Activity.java
首先在setContentView(XXX)的括号内容右击:


选择Generate:

选择Generate ButterKnife Injections:

 
 

到了这个界面可以看到列出了当前布局文件可以被find出来的控件,如果不需要再.java文件使用的控件可以不勾上,默认情况下控件命名与布局里的id名称一致。
我们再看左下角有一个Create ViewHolder,可以知道,ButterKnife也可以用在创建ViewHolder上,这里不再详述,接下来我们点击Confirm



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值