AndroidAnnotations首页

github上的项目地址AndroidAnnotations Github

wiki:https://github.com/excilys/androidannotations/wiki/AvailableAnnotations

1、使用依赖注入(Dependency Injection)不熟悉的可以了解一下Inversion of Control(IoC)

2、简化的线程模型(Simplified  threading model)

3、事件绑定(Event binding)

4、REST Client

5、No Magic  [它的意思是:AndroidAnnotations在编译的时候会产生一个子类(接下来你会明白),你查看这个子类,可以看到它是如何工作的]

项目中重要的两个jar包分别是:androidannotations-api-3.0.1.jar和androidannotations-3.0.1.jar
2).新建一个android项目,然后将androidannotations-api-3.0.1.jar复制到libs目录下,在项目的根目录新建一个文件夹,命名为compile-libs,然后将androidannotations-3.0.1.jar复制到该目录下.

3).然后设置项目属性:右键->Properties->Java Compiler->Annotation Processing 在该页面选中Enable project specific settings。

4).然后点击Annotation Processing的子项Factory Path页面,选中Enable project specific settings,然后添加编译所需的jar包。点击“Add JARs”将之前complie-libs目录下的androidannotations-3.0.1.jar导入,保存后退出。

运用方式之布局文件xml:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/myInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Click me!"
/>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

对应Activity的java文件:

 package com.example.testaa;

 import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById; import android.app.Activity;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
@ViewById(R.id.myInput)
EditText myEditText; @ViewById
Button myButton; @ViewById
TextView myTextView;
/**
* 加载完View之后进行的处理
*/
@AfterViews
void afterViewProcess(){
myButton.setText("Next");
myTextView.setText("第一个Activity页面");
} /**
* 绑定点击事件
*/
@Click(R.id.myButton)
void processClick(){
Intent intent=new Intent(this,SubActivity_.class);
intent.putExtra("input_value", myEditText.getEditableText().toString());
startActivity(intent);
} /**
* 利用UI线程显示一个Toast
* @param content
*/
@UiThread
void showToast(String content){
Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
}
/**
* 延时显示
* @param content
*/
@UiThread(delay=1000)
void showToastDelay(String content){
Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
}
}

可以看出,我们通过注解的方式,大大简化了原有的代码。

注解1:@ViewById 与findViewById功能相似,如果ViewById后没有设置资源ID的话,就是自动查找与变量名称相同的id资源(条件是控件变量名称要与xml中定义的id必须一致)。

但是这样会有一个问题,如果在Click()方法中调用,运行时就会报出:NullPointerException的错误,我们就不能在Click()方法中直接使用,而是要在@AfterView注释的方法中使用

注解2:@Click 点击事件处理的注解。

对于@Click,方法名和xml文件中的id一样就可以这样写,AndroidAnnotations会自动识别,对于多个Button,可以写多个@Click,也可以在这样:

@Click({R.id.button1,R.id.button2,R.id.button3})
void buttonClicked(Button bt){
//TODO ...
}

注解3:@UiThread 后台Ui线程的注解,省去了Handler等等。

注解4:@EActivity 提示Activity的注解,注意,该注解将Activity编译成Activity_,注意,多一个下划线“_”,因此在AndroidManifest.xml文件中需要将其添加下滑线

原因:使用AndroidAnnotations,编译的时候会生成一个子类,这个子类的名称就是在原来的类之后加了一个下划线“_”,比如这个例子产生的子类名称为“MyActivity_”,这就需要你在注册这个Activity的时候,在AndroidManifest.xml中将 MyActivity 改为 MyActivity_ ,使用的时候也是使用MyActivity_来表示此类,如从另一个Activity跳转到此节目就要这样用:

注解5:@AfterViews 是指View类注入完毕之后执行的代码。

我们第二个页面的布局文件与第一个相同,我们主要看一下它的java文件:

package com.example.testaa;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById; import android.app.Activity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; @EActivity(R.layout.activity_sub)
public class SubActivity extends Activity{
@ViewById(R.id.myInput)
EditText myEditText; @ViewById
Button myButton; @ViewById
TextView myTextView; @Extra(value="input_value")
String inputString; @AfterViews
void afterViewProcess(){
myTextView.setText(inputString);
} @Click(R.id.myButton)
void processClick(){
showToast("Clicked me !");
} @UiThread
void showToast(String content){
Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();
}
}

注:一个@Extra注解,这个注解的含义和getIntent().getExtra()相同,目的是获取上一个Activity通过Intent传递过来的值。

AndroidManifest.xml文件:

 <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testaa"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.testaa.MainActivity_"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.testaa.SubActivity_"></activity>
</application> </manifest>

注意:Activity的声明,多添加了下划线“_”

Android开源框架:AndroidAnnotations的更多相关文章

  1. Android 开源框架Universal-Image-Loader学习

    Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 Android 开源框架Universal-Image-Loader完全解析(二)--- 图片 ...

  2. Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/39057201),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

  3. Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存策略详解

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

  4. Android进阶笔记13:RoboBinding(实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架)

    1.RoboBinding RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架.从简单的角度看,他移除了如addXXListen ...

  5. android 开源框架推荐

    同事整理的 android 开源框架,个个都堪称经典.32 个赞! 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1)  JS ...

  6. Android 开源框架Universal-Image-Loader全然解析(二)--- 图片缓存策略具体解释

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

  7. Android开源框架Afinal第一篇——揭开圣女的面纱

    Android开源框架Afinal第一篇——揭开圣女的面纱 分类: Android开源框架哪点事2013-09-02 14:25 260人阅读 评论(0) 收藏 举报 Afinal 这是Afinal在 ...

  8. 六款值得推荐的Android开源框架简介

    技术不再多,知道一些常用的.不错的就够了.下面就是最近整理的“性价比”比较高的Android开源框架,应该是相对实用的. 1.volley 项目地址 https://github.com/smanik ...

  9. Android——开源框架Universal-Image-Loader + Fragment使用+轮播广告

    原文地址: Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 Android 开源框架Universal-Image-Loader完全解析(二) ...

  10. [转]Android开源框架ImageLoader的完美例子

    Android开源框架ImageLoader的完美例子 2013年8月19日开源框架之Universal_Image_Loader学习 很多人都在讨论如何让图片能在异步加载更加流畅,可以显示大量图片, ...

随机推荐

  1. STL之list容器用法

    List 容器 list是C++标准模版库(STL,Standard Template Library)中的部分内容.实际上,list容器就是一个双向链表,可以高效地进行插入删除元素. 使用list容 ...

  2. 浅析django的abstract,proxy, managed

    django.db.models.Model 的 Meta参数 参数 类型 说明 继承 abstract boolean 是否建表 不继承,子类自动充值为默认值(False) managed bool ...

  3. MySQL5.5 RPM安装的默认安装路径

    MySQL5.5 RPM安装的默认安装路径 2011-06-20 10:34:32|  分类: MySQL|举报|字号 订阅   下载LOFTER客户端     由于一客户要求安装mysql- 5.5 ...

  4. Git SSH Key 生成步骤

    it是分布式的代码管理工具,远程的代码管理是基于ssh的,所以要使用远程的git则需要ssh的配置. github的ssh配置如下: 一 . 设置git的user name和email: $ git ...

  5. 【Django】如何自定义manage.py命令? 达到启动后台进程的目的?

    代码: #-*- coding:utf- -*- """ The handle active user mail send """ from ...

  6. maven An error occurred while filtering resources

    转自:http://stackoverflow.com/questions/18145774/eclipse-an-error-occurred-while-filtering-resources m ...

  7. Java for LeetCode 042 Trapping Rain Water

    Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...

  8. codeforces B. Levko and Permutation 解题报告

    题目链接:http://codeforces.com/problemset/problem/361/B 题目意思:有n个数,这些数的范围是[1,n],并且每个数都是不相同的.你需要构造一个排列,使得这 ...

  9. 原创centos7安装hadoop2.7(转载请注明出处)

    启用ip vi /etc/sysconfig/network-scripts/ifcfg-ONBOOT=yes 编辑DNS /etc/resolv.conf nameserver 114.114.11 ...

  10. 解决 mysql 启动报错--发现系统错误2,系统找不到指定的文件

    HKEY_LOCAL_MACHINE-SYSTEM-CurrentControlSet-services-mysql(服务名)-ImagePath 更改为(自己的):"C:\Program ...