AndroidStudio插件 - ButterKnife的使用步骤
来源:互联网 发布:free mobile java在线 编辑:程序博客网 时间:2024/05/17 23:45
本篇为大家带来的是AndroidStudio下ButterKnife的插件使用,我们通常都是通过findviewbyid进行xml中视图查询,同时需要设置onClick进行点击事件监听,在这些琐碎却不可或缺的事情上,我们浪费了太多的时间,而ButterKnife的出现很大的提升了我们的效率。
在此之前,我也曾看过几篇关于ButterKnife的使用,总感觉有一些瑕疵,很容易会让新手卡壳,那么下面,我将为大家图文并茂的讲解ButterKnife的具体使用步骤。
1.首先在 build.gradle 中添加ButterKnife的依赖:
compile 'com.jakewharton:butterknife:6.1.0'
2.Ctrl+Alt+S 打开设置中Plugins进行ButterKnife插件下载(大家可选取下载次数最多的插件,因为相对稳定),之后进行安装,重启AndroidStudio:
3.如果上面大家不会下载,可以查看上一篇插件的讲解,大家可通过链接:http://blog.csdn.net/qq_20451879/article/details/55001826进行学习,再此我们下载好之后,准备工作就已经做完了,下面我们开始正式进行ButterKnife的使用
注意:这里我们要注意先行选取当前的layout布局,图片中已经使用红色边框标注,然后Code下进行Generate选取
在弹出下面的选项中“我们选取生成ButterKnife 得注解”
4.这里我们可以看到,我们在findbyid之后同时可以为相应的id设置点击事件,同时可以设置自己熟悉的变量名
生成的findbyid:
生成的点击事件:
5.让我们正式运行一下我们第一次使用ButterKnife的项目:
看来我们的结果是没有问题的,下面代码附送大家(之后有时间会再次写一些关于ButterKnife的补充知识):
MainActivity Code:
package com.example.administrator.ButterKnifedemo;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.Toast;import butterknife.ButterKnife;import butterknife.InjectView;import butterknife.OnClick;public class MainActivity extends AppCompatActivity { @InjectView(R.id.my_text_view) TextView mText; @InjectView(R.id.activity_main) RelativeLayout activityMain; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.inject(this); } //这里实现了点击事件 @OnClick(R.id.my_text_view) public void onClick() { Toast.makeText(this,"ButterKnife点击事件",Toast.LENGTH_LONG).show();; } //如果需要重置,我们需要在onDestroy生命周期进行重新设置 @Override protected void onDestroy() { ButterKnife.reset(null); super.onDestroy(); }}
MainActivity Xml Code:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" 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="com.example.administrator.cardviewdemo.MainActivity"> <TextView android:id="@+id/my_text_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="Butter Knife 使用" /></RelativeLayout>
build.gradle Code:
apply plugin: 'com.android.application'android { compileSdkVersion 23 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.example.administrator.ButterKnifedemo" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.jakewharton:butterknife:6.1.0' compile 'com.android.support:appcompat-v7:23.4.0' compile 'com.android.support:support-v4:23.4.0' testCompile 'junit:junit:4.12'}
- AndroidStudio插件 - ButterKnife的使用步骤
- AndroidStudio插件ButterKnife的使用(库和插件)
- AndroidStudio使用偷懒插件Butterknife和GsonFormat
- ButterKnife的使用步骤
- ButterKnife插件的使用
- ButterKnife插件的使用
- Android之ButterKnife在AndroidStudio中的使用步骤
- androidstudio Butterknife-7.0.1的配置使用
- AndroidStudio使用butterknife的详细介绍
- AndroidStudio下ButterKnife的集成和使用
- AndroidStudio使用Butterknife
- AndroidStudio中使用ButterKnife注解
- AndroidStudio中使用ButterKnife教程!
- AndroidStudio中如何使用ButterKnife
- butterknife 使用步骤
- 在AndroidStudio中添加butterknife插件
- androidstudio 插件 butterknife 安装及应用
- Studio 工具插件ButterKnife的使用
- 设计模式之观察者模式
- linux安装telnet服务端客户端
- git 常用命令
- Max Sum
- Android Material Design系列之主题样式介绍说明等
- AndroidStudio插件 - ButterKnife的使用步骤
- Android Content Provider使用
- android.content.res.Resources$NotFoundException: String resource ID #0x2f38
- SystemUI之控制中心实现
- sitemesh
- iOS中的单例模式
- Hololens的凝视交互
- EJB配置详解
- SystemUI之启动过程整理