服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android Studio中ButterKnife插件的安装与使用详解

Android Studio中ButterKnife插件的安装与使用详解

2021-03-14 15:19cxc19890214 Android

本篇文章主要介绍了Android Studio中ButterKnife插件的安装与使用详解,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

1》android studio 安装butterknife插件

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

1.1》打开plugins界面

Android Studio中ButterKnife插件的安装与使用详解

Android Studio中ButterKnife插件的安装与使用详解

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

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

2.1》在bulid.gradle中添加依赖

Android Studio中ButterKnife插件的安装与使用详解

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

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

2.2.1》示例布局文件如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:paddingbottom="@dimen/activity_vertical_margin"
  android:paddingleft="@dimen/activity_horizontal_margin"
  android:paddingright="@dimen/activity_horizontal_margin"
  android:paddingtop="@dimen/activity_vertical_margin"
  tools:context=".mainactivity">
 
  <textview
    android:id="@+id/text_veiw_tv1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="textview 1" />
 
  <button
    android:id="@+id/button_bt1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="button1" />
 
  <textview
 
    android:id="@+id/text_veiw_tv2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="textview 2" />
 
 
  <button
    android:id="@+id/button_bt2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="button2" />
 
 
</linearlayout>

2.2.2》在代码中使用注解

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

Android Studio中ButterKnife插件的安装与使用详解

Android Studio中ButterKnife插件的安装与使用详解

Android Studio中ButterKnife插件的安装与使用详解

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

如下所示:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@bind(r.id.text_veiw_tv1)
textview textveiwtv1;
@bind(r.id.text_veiw_tv2)
textview textveiwtv2;
@bind(r.id.button_bt1)
button buttonbt1;
@bind(r.id.button_bt2)
button buttonbt2;
 
@override
protected void oncreate(bundle savedinstancestate) {
  super.oncreate(savedinstancestate);
  setcontentview(r.layout.activity_main);
  butterknife.bind(this);
 
 
}

标注如下:

Android Studio中ButterKnife插件的安装与使用详解

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/cxc19890214/article/details/47430547

延伸 · 阅读

精彩推荐