Android第三方注解框架Annotations和butterknife
来源:互联网 发布:数据挖掘十大经典算法 编辑:程序博客网 时间:2024/06/05 01:15
使用注解,可以帮我们去实现一些简单而重复的工作,使得开发效率得到有效提升,免去不必要的时间花费。像findViewById,既可以省时间,也省去了代码量。
AndroidAnnotations
AndroidAnnotations是一个开源的注解框架,可以加快Android开发。通过它,让你专注于你真正重要的。通过简化你的代码,更有利于它的维护。
主 页: http://androidannotations.org/
Github 主页: https://github.com/excilys/androidannotations
一.配置开发环境
此配置为Android studio的配置
在project的build.gradle文件中,添加以下部分
// Top-level build file where you can add configuration options common to all sub-projects/modules.buildscript { repositories { jcenter() mavenCentral() //添加的部分 } dependencies { classpath 'com.android.tools.build:gradle:1.3.0' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' //添加依赖 // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }}//添加的部分---------->>repositories { mavenCentral() mavenLocal()}// <<----------------allprojects { repositories { jcenter() }}task clean(type: Delete) { delete rootProject.buildDir}
在需要用到此框架的module的build.gradle文件中,添加以下部分
apply plugin: 'com.android.application'apply plugin: 'android-apt' //添加的部分def AAVersion = '3.3.2' //添加的部分,自定义了一个变量“版本号”android { compileSdkVersion 23 buildToolsVersion "23.0.0" defaultConfig { applicationId "com.ql.annotationsdemo" minSdkVersion 10 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } }}dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.0' compile 'com.android.support:design:23.1.0' apt "org.androidannotations:androidannotations:$AAVersion" //添加依赖 compile "org.androidannotations:androidannotations-api:$AAVersion" //添加依赖 compile 'com.jakewharton:butterknife:5.1.1'}//添加的部分------------->>>apt { arguments { androidManifestFile variant.outputs[0].processResources.manifestFile // if you have multiple outputs (when using splits), you may want to have other index than 0 // you should set your package name here if you are using different application IDs // resourcePackageName "your.package.name" // You can set optional annotation processing options here, like these commented options: // logLevel 'INFO' // logFile '/var/log/aa.log' }}// <<<-------------------
成功后即可在代码中使用
二.常用标签
标签 | 等同于
@EActivity
setContentView 绑定布局视图 @ViewById
findViewById @Click
setOnClickListener 单击事件 @Extra
intent.getExtra 活动之间的intent传值 @AfterViews
View初始化完成 @ItemClick
ListView Item单击事件 @ItemLongClick
ListView Item长按事件 @Background
后台运行 @UIThread
前台运行
三.注意:
1.AndroidManifest.xml中注册的activity要在原类名后追加下划线“_”
2.使用注解的控件和方法不能使用private修饰符
3.使用 @Click
@ItemClick
@ItemLongClick
标签必须指明ID
4.控件设置初始值要在生命周期的onResume()方法中或者使用@AfterViews
标签
5.使用@EActivity
标签绑定布局视图必须要在class的外面(跟import同一级)
四.示例
import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.widget.TextView;import org.androidannotations.annotations.AfterViews;import org.androidannotations.annotations.Click;import org.androidannotations.annotations.EActivity;import org.androidannotations.annotations.ViewById;@EActivity(R.layout.activity_main)public class MainActivity extends AppCompatActivity { public static final String EXTRA_KEY_NAME = "name"; public static final String EXTRA_KEY_AGE = "age"; @ViewById(R.id.textView) TextView textView; @ViewById TextView textView2; @Click(R.id.button) public void doSomething() { Intent intent = new Intent(this, BActivity_.class); intent.putExtra(EXTRA_KEY_NAME, "zhangsan"); intent.putExtra(EXTRA_KEY_AGE, 16); startActivity(intent); } @Override protected void onResume() { super.onResume(); //设置值方式1// textView.setText("this is from annotation"); } //设置值方式2 @AfterViews public void setcontent(){ textView.setText("this is from annotation"); textView2.setText("this is textview2"); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); }}
BActivity.java
@EActivity(R.layout.activity_b)public class BActivity extends Activity { @Extra(MainActivity.EXTRA_KEY_NAME) String name; @Extra(MainActivity.EXTRA_KEY_AGE) int age; @ViewById TextView tv_name; @ViewById TextView tv_age; @Override protected void onResume() { super.onResume(); tv_name.setText(name); tv_age.setText(age + ""); }}
butterknife
相比于AndroidAnnotations,功能更少,使用却比较方便,用于做findViewById工作。
1.使用步骤
1.添加依赖
在需要用到的module中的build.gradle文件中
compile 'com.jakewharton:butterknife:5.1.1'
或
2.安装插件
选择在线安装,安装完成重启一下
3.使用
在活动中将布局单词选中或者将光标置于单词中,鼠标右键选择Generate。或者使用键盘Alt + insert。会看到Generate butterknife injections这一项。点击确定,即可列出布局文件中所有带ID的控件
4.示例
5.注意
布局文件中的控件必须要指定ID,才会将其显示列出
- Android第三方注解框架Annotations和butterknife
- Android第三方注解框架Annotations详解
- Android-第三方框架ButterKnife(注解框架)
- android 注解框架 butterKnife 和AndroidAnnotations
- Android butterknife注解框架
- Android ButterKnife注解框架
- android注解框架--ButterKnife
- Android主流IOC框架浅析(注解反射,Annotations,ButterKnife的简单使用)
- Android 反射Annotations和Butterknife解析
- Android开发 注解butterknife框架和权限permissionsdispatcher框架冲突
- Android Butterknife框架 注解攻略
- Android butterknife框架 注解攻略
- Android butterknife框架 注解攻略
- Android Butterknife框架 注解攻略:
- android注解框架ButterKnife学习
- Android ButterKnife注解框架使用
- Android ButterKnife 注解框架的使用详解和原理分析
- Android开发第三方库 -- ButterKnife
- SSH的编码 和 ajax乱码问题
- vi编辑器
- 2011 Asia Dalian Regional Contest
- xml 将xml文档以表格的形式输出
- 线索二叉树和中序非递归遍历线索化后的二叉树
- Android第三方注解框架Annotations和butterknife
- 作業系統之前的程式 for stm32f4discovery (3) - usart
- C++STL学习(10)STL算法(1)
- “适配器模式”的应用
- maven-learning03
- MySQL的一些备忘技巧
- 关于ellipsize属性使用的一些细节
- 为什么要继承
- cf 14a Letter