Android开源框架: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的更多相关文章
- Android 开源框架Universal-Image-Loader学习
Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 Android 开源框架Universal-Image-Loader完全解析(二)--- 图片 ...
- Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/39057201),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...
- Android 开源框架Universal-Image-Loader完全解析(二)--- 图片缓存策略详解
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...
- Android进阶笔记13:RoboBinding(实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架)
1.RoboBinding RoboBinding是一个实现了数据绑定 Presentation Model(MVVM) 模式的Android开源框架.从简单的角度看,他移除了如addXXListen ...
- android 开源框架推荐
同事整理的 android 开源框架,个个都堪称经典.32 个赞! 1.volley 项目地址 https://github.com/smanikandan14/Volley-demo (1) JS ...
- Android 开源框架Universal-Image-Loader全然解析(二)--- 图片缓存策略具体解释
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...
- Android开源框架Afinal第一篇——揭开圣女的面纱
Android开源框架Afinal第一篇——揭开圣女的面纱 分类: Android开源框架哪点事2013-09-02 14:25 260人阅读 评论(0) 收藏 举报 Afinal 这是Afinal在 ...
- 六款值得推荐的Android开源框架简介
技术不再多,知道一些常用的.不错的就够了.下面就是最近整理的“性价比”比较高的Android开源框架,应该是相对实用的. 1.volley 项目地址 https://github.com/smanik ...
- Android——开源框架Universal-Image-Loader + Fragment使用+轮播广告
原文地址: Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 Android 开源框架Universal-Image-Loader完全解析(二) ...
- [转]Android开源框架ImageLoader的完美例子
Android开源框架ImageLoader的完美例子 2013年8月19日开源框架之Universal_Image_Loader学习 很多人都在讨论如何让图片能在异步加载更加流畅,可以显示大量图片, ...
随机推荐
- Coursera台大机器学习课程笔记11 -- Nonlinear Transformation
这一节讲的是如何将线性不可分的情况转为非线性可分以及转换的代价.特征转换是机器学习的重点. 最后得出重要的结论是,在做转换时,先从简单模型,再到复杂模型. 参考:http://www.cnblogs. ...
- delphi 枚举类型
枚举类型定义了一系列有序值的集合.枚举变量就是从这个既定的集合中取某个值.集合中的有序值可以称为元素,元素一般从0开始索引(也就是元素的顺序号). 定义一个枚举类型,采用以下的格式: type typ ...
- poj1258 Agri-Net 最小生成树
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 44032 Accepted: 18001 Descri ...
- Telegram
官网:https://www.telegram.org/ GitHub:https://github.com/telegramdesktop/tdesktop
- FileOutputStream与FileInputStream互相转换
List<InstorageNoticeDto> noticeList = null; FileOutputStream fos = null; FileInputStream is = ...
- git无法clone远程代码库及git代理设置
git作为一个版本管理神器,日常工作中自然也就少不了了:特别是Android开发,github和google是逃不过的了.然而很多时候需要用到git克隆远程的代码库,众所周知的原因google.and ...
- Windows下配置Apache服务器并支持php
php环境的配置相对来说比较繁琐,网上教程大部分都是放一起说,总体感觉比较乱,其实Apache是一款通用的服务器软件,可以用来配置支持静态页面,php.Python.Java甚至asp等服务端语言,要 ...
- 安装tar.bz2文件
(1) 解包 – tar jxvf softname-10.0.1.tar.gz -C /usr/src/(-C指的是把文件解压到后面的路径下,此处可以不选) – cd /usr/src/softna ...
- 【数据结构】Huffman树
参照书上写的Huffman树的代码 结构用的是线性存储的结构 不是二叉链表 里面要用到查找最小和第二小 理论上锦标赛法比较好 但是实现好麻烦啊 考虑到数据量不是很大 就直接用比较笨的先找最小 去掉最小 ...
- VS 高亮显示不带后缀的C++头文件
工具-选项-文本编辑器-文件扩展名-勾选“将无扩展名文件映射到(M)” Microsoft Visual C++