Android注解框架Annotations从配置到应用

最近在看一个新的基于注解开发的项目,了解到Android中同样存在一些注解框架,能够大大提高我们的编码规范和效率,减少代码量。本文主要分为两部分,针对较热门的注解框架Android Annotations在Android Studio上的配置进行详解。

 

Android Annotations配置

1.首先,一个框架需要有它的依赖包,Annotations框架需要使用两个jar包,我已经上传在了我的资源上,点此下载
 

2.在新建的普通项目里面,新建一个compile-libs文件夹,将下载来的androidannotation-xx.jar添加到compile-libs文件夹下,将androidannotation-api-xx.jar添加到libs文件夹下,如图:

Android注解框架Annotations从配置到应用

 

3.添加完依赖包之后,还要为其配置,在Project的build.gradle下的dependencies节点添加:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4',如图:

Android注解框架Annotations从配置到应用

 

4.在Moudle的build.gradle下添加:
apply节点下:
apply plugin: 'android-apt'
def AAVersion = '3.2+'
-----------------------------------------------------------------------------
dependencies节点下:
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
compile 'com.android.support:support-v4:23.4.0'
------------------------------------------------------------------------------
添加一段apt:
apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
        resourcePackageName 'com.example.zjy.myapplication'
    }
}
如图:

Android注解框架Annotations从配置到应用

Android注解框架Annotations从配置到应用

 

5.以上完成了基本的搭建,接下来还需要注意一点,AndroidAnnotations框架有一个特点,就是在编译时会将我们的组件名后面加上一个下划线,比如MainActivity就变为MainActivity_.class,所以如果我们要通过这个框架来进行开发,那需要在使用这些组件的时候都遵循这种规范,如图,在AndroidMainfest.xml文件中:

Android注解框架Annotations从配置到应用

 

6.但是添加完之后你会发现项目报错,识别不到xxxActivity_这个类名,那是因为还没有编译,需要将项目进行编译,点击工具栏中的编译按钮(就图案是010101,在菜单栏Build下方的那个按钮):

Android注解框架Annotations从配置到应用

 

运行项目若报错:
com.android.builder.packaging.duplicateFileException

Android注解框架Annotations从配置到应用

则根据所指的重复文件的路径,将在build.gradle文件的android节点中添加:
 packagingOptions {
        exclude 'META-INF/maven/org.androidannotations/androidannotations-api/pom.xml'
        exclude 'META-INF/maven/org.androidannotations/androidannotations-api/pom.properties'
        exclude 'androidannotations-api.properties'
 }

 

再次运行,完美通过。

 

 

Android Annotations应用

AndroidAnnotations的基本应用:

[email protected](R.id.xxx)

这是最为基本的一个注解,表示该类是一个Activity,并且对应于哪个布局文件

 

 

 
  1. @EActivity(R.layout.activity_main)

  2. public class MainActivity extends Activity {

  3.  
  4. }

 

 

可以看到,我们没有了onCreate方法,也没有了setContentView方法,依然能够运行出界面。

 

 

[email protected](R.id.xxx)

作用:相当于findViewById(R.id.xxx)。
在布局文件中定义一个按钮:

 

 

 
  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:fitsSystemWindows="true"

  6. >

  7.  
  8. <Button

  9. android:id="@+id/btn"

  10. android:layout_width="match_parent"

  11. android:layout_height="wrap_content" />

  12.  
  13. </LinearLayout>

 

 

在Activity中通过注解声明该按钮,注意按钮要声明为public不能为private:

 

 
  1. @EActivity(R.layout.activity_main)

  2. public class MainActivity extends Activity {

  3.  
  4. @ViewById(R.id.btn)

  5. public Button btn;

  6.  
  7. }

 

 

 

 

[email protected](R.id.xxx)

作用:为该按钮注册监听事件,表明该方法为该按钮的点击事件方法

 

 

 
  1. @EActivity(R.layout.activity_main)

  2. public class MainActivity extends Activity {

  3.  
  4. @ViewById(R.id.btn)

  5. public Button btn;

  6.  
  7. @Click(R.id.btn)

  8. void myClick(){

  9. Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();

  10. }

  11.  
  12. }

 

还可以对多个button进行注解,再通过switch进行判断:

 

 
  1. @Click({R.id.button1,R.id.button2,R.id.button3})

  2. void buttonClicked(Button btn){

  3. switch(btn.getId()){

  4. case R.id.button1:

  5. //这里实现button1的点击事件

  6. break;

  7. ...

  8. }

  9. }

 

 

 

 

[email protected]

作用:只有所有View都注入完毕之后,才会执行该注解的方法,一般进行一些初始化的操作

 

 

 
  1. @EActivity(R.layout.activity_main)

  2. public class MainActivity extends Activity {

  3.  
  4. @ViewById(R.id.btn)

  5. public Button btn;

  6.  
  7. @Click(R.id.btn)

  8. void myClick(){

  9. Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();

  10. }

  11.  
  12. @AfterViews

  13. void init(){

  14. btn.setText("按钮注入完毕后才执行此方法");

  15. }

  16.  
  17. }

 

 

 

 

[email protected](String params)

作用:获得其它地方跳转过来时所传递过来的数据,括号中的字符串即为传递过来的数据的key
如下例子:

 

MainActivity中:

 

 
  1. Intent intent = new Intent(MainActivity.this,SecondActivity_.class);

  2. Bundle bundle = new Bundle();

  3. bundle.putString("hello","hello");

  4. intent.putExtras(bundle);

  5. startActivity(intent);

 

SecondActivity:

 

 
  1. @EActivity(R.layout.activity_second)

  2. public class SecondActivity extends Activity{

  3.  
  4. @Extra("hello")

  5. String params;

  6.  
  7. @AfterViews

  8. void init(){

  9. Toast.makeText(this,params,Toast.LENGTH_SHORT).show();

  10. }

  11. }

 

跳转的时候注意加下划线:

 

 
  1. Intent intent = new Intent(MainActivity.this,SecondActivity_.class);

  2. startActivity(intent);

 

 

 

 

[email protected]和@UiThread

@Background作用:相当于用来进行后台操作的方法(比如请求网络资源,操作文件等等),被Backgroung注解的方法里面不能进行任何UI操作,否则会报错。

 

 

 
  1. @Background

  2. void httprequest(){

  3. //进行http操作

  4. //操作数据库

  5. //操作文件

  6. }

 

 

@UiThread作用:相当于用来进行UI控件的操作,被UiThread注解的方法里面不能进行任何后台操作,否则会报错

 

 
  1. @UiThread

  2. void runinUI(){

  3. Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();

  4. }

 

如何将两者结合使用:

 

 
  1. @AfterViews

  2. void init(){

  3. httprequest();

  4. }

  5.  
  6. @Background

  7. void httprequest(){

  8. //进行http操作

  9. //操作数据库

  10. //操作文件

  11.  
  12. //执行完后台操作之后再执行UI操作

  13. runinUI();

  14. }

  15.  
  16. @UiThread

  17. void runinUI(){

  18. Toast.makeText(this,"UiThread",Toast.LENGTH_SHORT).show();

  19. }

 

 

 

 

[email protected]

作用:用来标识该类是一个Fragment,并对应哪个布局文件

 

 

 
  1. @EFragment(R.layout.layout_myfragment)

  2. public class MyFragment extends Fragment{

  3.  
  4. }

 

 

 

 

[email protected]

作用:用来给当前Fragment接收别的地方传来的数据
MainActivity中:

 

 

 
  1. @EActivity(R.layout.activity_main)

  2. public class MainActivity extends Activity {

  3.  
  4. @ViewById(R.id.btn)

  5. public Button btn;

  6.  
  7. @Click(R.id.btn)

  8. void myClick(){

  9. MyFragment fragment = new MyFragment_();

  10. Bundle bundle = new Bundle();

  11. bundle.putString("hello","...");

  12. //设置传递的参数

  13. fragment.setArguments(bundle);

  14. FragmentTransaction transaction = getFragmentManager().beginTransaction();

  15. transaction.add(R.id.content, fragment);

  16. transaction.commit();

  17. }

  18. }

 

activity_main.xml:

 

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. android:layout_width="match_parent"

  3. android:layout_height="match_parent"

  4. android:orientation="vertical"

  5. >

  6.  
  7. <Button

  8. android:id="@+id/btn"

  9. android:layout_width="match_parent"

  10. android:layout_height="wrap_content" />

  11. <FrameLayout

  12. android:id="@+id/content"

  13. android:layout_width="match_parent"

  14. android:layout_height="wrap_content">

  15. </FrameLayout>

  16.  
  17. </LinearLayout>

 

 

MyFragment类:

 

 
  1. @EFragment(R.layout.layout_myfragment)

  2. public class MyFragment extends Fragment{

  3.  
  4.  
  5. @FragmentArg("hello")

  6. String params;

  7.  
  8. @AfterViews

  9. void init(){

  10. Toast.makeText(getActivity(), params, Toast.LENGTH_SHORT).show();

  11. }

  12.  
  13. }

 

layout_myfragment.xml:

 

 
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  2. android:orientation="vertical"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. >

  6. </LinearLayout>

 

 

注意:FragmentArg括号中的参数要跟bundle所装载的数据的key一致,才能接受到传过来的数据。

 

以上介绍了比较常用的几种注解,Annotaions框架还提供了很多其他的注解,如下:

@AfterInject 定义的方法在类的构造方法执行后执行

@AfterTextChange定义的方法在TextView及其子类的Text属性改变后执行

@BeforeTextChange 定义的方法在TextView及其子类的Text属性改变前执行

@EProvider 在 ContentProvider中启用Annotations

@EReceive 在BroadcastReceiver中启用Annotations

@EService 在Service中启用Annotations

@EView 在自定义的View的子类中启用Annotations

 

......等等,有兴趣的朋友可以去看Annotations的官方API文档

 

 

总结:Annotations确实为我们的开发带了许多方便,特别是在大型的项目下,不再需要一遍遍的findViewById,不再需要一遍遍地setContentView,同时另一方面又提高了代码的简洁度和可读性,所以相信其运用到实际开发中是非常有帮助的。