首先AndroidAnnotations是一个比较全面的注解框架。

接下来看一下官方解释:
Fast Android Development. Easy maintenance.
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开发,易于维护。
AndroidAnnotations是一个能让你快速进行Android开发的开源框架。它使代码更加精简,让你专注于真正重要的地方。通过简化你的代码,使项目更容易维护。

1.下载AndroidAnnotations

Github下载:
https://github.com/excilys/androidannotations

2.引入AndroidAnnotations

开发环境使用Android Studio

(1)在build.gradle文件中添加依赖,如下:

apply plugin: 'com.android.application'// AndroidAnnotationsapply plugin: 'com.neenbedankt.android-apt'def AAVersion = '4.0.0'android {    ...}dependencies {    compile fileTree(dir: 'libs', include: ['*.jar'])    // AndroidAnnotations    apt "org.androidannotations:androidannotations:$AAVersion"    compile "org.androidannotations:androidannotations-api:$AAVersion"}// apt settingsapt {    arguments {        androidManifestFile variant.outputs[0].processResources.manifestFile    }}

3.注入Avtivity

来看一个Activity:

public class MainActivity extends Activity {    TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        textView = (TextView) findViewById(R.id.textView);        // 初始化View        initView();    }    public void initView() {       ...    }}

注入后:

@EActivity(R.layout.activity_main)public class MainActivity extends Activity {    @ViewById(R.id.textView)    TextView textView;    @AfterViews    void initView() {        ...    }}

注意:需要把AndroidManifest.xml中MainActivity改成MainActivity_,这是AndroidAnnotations一个特别的地方,改完后记得build一下工程。

<activity android:name=".MainActivity" /> 改为:<activity android:name=".MainActivity_" />

4.注入点击事件

来看一个Activity:

public class MainActivity extends Activity implements View.OnClickListener {    Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button = (Button) findViewById(R.id.button);        button.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch(v.getId()) {            case R.id.button:                Intent intent = new Intent(this, TwoActivity.class);                intent.putExtra("message", "haha!");                startActivity(intent);                break;        }    }}

注入后 implements View.OnClickListener 这些就不需要写了:

@EActivity(R.layout.activity_main)public class MainActivity extends Activity {    @ViewById(R.id.button)    Button button;    @Click(R.id.button)    void buttonAction(View view) {        TwoActivity_.intent(this).msg("haha!").start();    }}

6.深入AndroidAnnotations源码学习

我们来看一下官方的Overview:
AndroidAnnotations works in a very simple way. It automatically adds an extra compilation step that generates source code, using the standard Java Annotation Processing Tool.
What source code ? For each enhanced class, for example each @EActivity annotated activity, a subclass of this activity is generated, with the same name plus an underscore appended at the end.
For instance, the following class:

package com.some.company;@EActivitypublic class MyActivity extends Activity {  // ...}

Will generate the following subclass, in the same package but in another source folder:

package com.some.company;public final class MyActivity_ extends MyActivity {  // ...}

This subclass adds behavior to your activity by overriding some methods (for instance onCreate()), yet delegating the calls to super.

That is the reason why you must add _ to your activity names in AndroidManifest.xml:

<activity android:name=".MyListActivity_" />

下来简单翻译一下:
AndroidAnnotations是一个非常简单的工作方式。它会自动使用Java的APT(标准的java注释处理工具,jdk1.6引入的特性)增加一个额外的编译步骤生成Java源代码。
什么源代码?每个增强类,对于每个标注@EActivity注释的Activity会生成一个子类,这个子类是带下划线的并继承来自于您自己的Activity。
例如,下面的类:

package com.some.company;@EActivitypublic class MyActivity extends Activity {  // ...}

将生成下面的子类,在同一个包中,但在另一个源文件夹中:

package com.some.company;public final class MyActivity_ extends MyActivity {  // ...}

该类通过重写一些方法(比如oncreate())添加行为活动,但委托调用父类。

这就是为什么必须要在AndroidManifest.xml中添加XxxActivity_的原因:

<activity android:name=".MyListActivity_" />

更多相关文章

  1. 可以下载Android 源代码的repo源文件
  2. 【实战】android网页源代码查看器
  3. Android源代码查看途径
  4. Android 布局加载源代码分析
  5. Android系统源代码下载
  6. Android用户界面UI组件--AdapterView及其子类(三) ExpandableLis
  7. android 手机管理软件 发布开源代码
  8. Android Activity 及其子类

随机推荐

  1. Android启动过程简析
  2. Android学习之Adapter(适配器)源代码分析
  3. Android 核心已经从 Linux kernel 代码库
  4. MQTT+ApolloServer实现Android的消息推送
  5. adb的一些命令
  6. android - 为安全而设计 - 3 - 开发文档
  7. [原]零基础学习在Android进行SDL开发系列
  8. Android(安卓)JNI和NDK学习(4)--编译与预
  9. android中分页加载的实现:)
  10. Android:Intent