欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-01-08 12:18:08|栏目:JAVA代码|点击:

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》示例布局文件如下:

<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》在代码中使用注解

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




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

如下所示:

@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); 
 
 
} 

标注如下:


上一篇:详解spring cloud hystrix请求缓存(request cache)

栏    目:JAVA代码

下一篇:linux下用renameTo方法修改java web项目中文件夹名称的实例

本文标题:Android Studio中ButterKnife插件的安装与使用详解

本文地址:http://www.codeinn.net/misctech/42047.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有