AndroidAnnotations--提高开发效率的利器
2015-08-02 21:09
477 查看
AndroidAnnotations–提高开发效率的利器
今天刚好有时间能够呆在自己的房间里,对之前写的代码进行了重构,发现我之前写的代码显得很是冗余,很多语句显得啰嗦,我开始寻找一种简易的方式去处理这些代码(其实是想偷懒哈…),于是就发现了一款利器:AndroidAnnotations框架,堪称是懒人开发的利器.借用 官网 的解释来说明一下AndroidAnnotations框架的概念
AndroidAnnotations is an Open Source framework that speeds up Android development. It takes care of the plumbing, and lets you concentrate on what’s really important. By simplifying your code, it facilitates its maintenance.
下面就直奔主题吧,主要是想记录一下自己的学习笔记,以后方便查看.
我使用的环境是在android studio中,所以就只围绕android studio的相关配置操作来写了…
首先是对AndroidAnnotations的环境配置
1.在build.gradle文件中的配置信息://(project中) buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:1.2.3' classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' } } allprojects { repositories { mavenCentral() mavenLocal() } } //(module中) apply plugin: 'com.android.application' apply plugin: 'android-apt' def AAVersion = '3.2' android { compileSdkVersion 22 buildToolsVersion "23.0.0 rc3" defaultConfig { applicationId "com.asmine.wyb.annotation" minSdkVersion 15 targetSdkVersion 22 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.1' apt "org.androidannotations:androidannotations:$AAVersion" compile "org.androidannotations:androidannotations-api:$AAVersion" } 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' } }
在android studio中有关androidannotation的配置就是这么多,接下来只要 Sync project即可使用强大的AndroidAnnotation框架了.
一个简单的示例程序
首先来看看layout文件的内容:<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" tools:context=".MainActivity"> <TextView android:id="@+id/text" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="30sp"/> <Button android:id="@+id/btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="button" android:textSize="25sp"/> </LinearLayout>
接下来就轮到今天的主角上场了:
package com.asmine.wyb.annotation; import android.app.Activity; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import org.androidannotations.annotations.Click; import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.LongClick; import org.androidannotations.annotations.ViewById; @EActivity(R.layout.activity_main) public class MainActivity extends Activity { @ViewById(R.id.text) TextView textView; @ViewById(R.id.btn) Button button; @Click({R.id.text,R.id.btn}) public void click(){ showToast("click"); textView.setText("click..."); } @LongClick({R.id.text,R.id.btn}) public void longClick(){ showToast("long click..."); textView.setText("long click..."); } void showToast(String msg){ Toast.makeText(this,msg,Toast.LENGTH_LONG).show(); } }
看起来是不是要比普通的代码要简洁很多呢?我想是这样的.
接下来解释一下其中用到的一些注解方式和用法:
@EActivity(layoutId):被此注解标注的activity将会被AndroidAnnotation解析到,其中的参数必须是一个有效的layout id,此注解等同于在onCreate()方法中的setContentView()方法.
@ViewById(viewId):此注解等同于findViewById()方法,你也可以不指定viewId,前提是变量名要跟viewId一致
@Click(viewId):此注解声明了对viewId所对应的View的Click监听事件,相当于setOnClickListener()方法
@LongClick(viewId):此注解声明了对view的longClick事件的监听
@AfterViews:此注解说明在所有View初始化完成之后进行的操作
这里只是列举了AndroidAnnotations框架所支持的一部分注解,更多的注解方式请查看CookBook 以及 AvailableAnnotations,这里面列举了所有支持的注解方式.
最后需要注意的事情是需要在AndroidManifest.xml文件中声明此Activity ,但是请注意的是,声明的activity文件名不是MainActivity,而是MainActivity_,需要在你定义的activity的名称后面加上 “_” ,理由是什么呢?原因是 AndroidAnnotations 框架会根据 @EActivity 所声明的activity生成一个子类,子类的名称则是在该activity的名称后面加上”_”,实际上起作用的是这个子类,所以我们需要声明的是这个子类,而不是你自己定义的activity名称,这一点需要特别注意.想更清楚的了解其中的原理,可以查看源代码去了解其中的机制原理
这里仅仅介绍了AndroidAnnotations框架的一小部分内容,还有更多的内容需要我们自己去学习,我也是刚刚接触这个框架,还需要一些时间去熟悉掌握这个框架,我想,学好这个框架一定会简化我们的代码,使我们的代码更容易维护和理解,可以减少很多繁琐但是又不得不做的事情.
相关文章推荐
- android textview背景色动画
- Android 处理用户被迫下线
- Android SDK在线更新镜像服务器
- Android Studio——Gradle 版本回退
- Android动态逆向分析工具ZjDroid--脱壳神器
- Android四大组件之Service(一)
- android 使用两个surfaceview 在摄像机画面上绘图
- android opencv 人脸检测
- Android之路
- Android基础知识之四大组件Activity(三)多个Activity交互的生命周期与设计思想
- Service和Thread的关系
- Android开源框架(一):AndroidAnnotations
- Android-单选按钮RadioButton和复选框checkbox
- android BaseAdapter的getView的调用时机
- Android 测试技能树
- Android 存储学习之使用SharedPreference保存文件
- Android 常用 adb 命令总结
- Android SensorManager getOrientation getRotationMatrix
- Android实例-路径信息及文件和文件夹的操作(XE8+小米2)
- Android瀑布流照片墙实现,体验不规则排列的美感