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文件夹下,如图:
3.添加完依赖包之后,还要为其配置,在Project的build.gradle下的dependencies节点添加:
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4',如图:
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'
}
}
如图:
5.以上完成了基本的搭建,接下来还需要注意一点,AndroidAnnotations框架有一个特点,就是在编译时会将我们的组件名后面加上一个下划线,比如MainActivity就变为MainActivity_.class,所以如果我们要通过这个框架来进行开发,那需要在使用这些组件的时候都遵循这种规范,如图,在AndroidMainfest.xml文件中:
6.但是添加完之后你会发现项目报错,识别不到xxxActivity_这个类名,那是因为还没有编译,需要将项目进行编译,点击工具栏中的编译按钮(就图案是010101,在菜单栏Build下方的那个按钮):
运行项目若报错:
com.android.builder.packaging.duplicateFileException
则根据所指的重复文件的路径,将在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,并且对应于哪个布局文件
-
@EActivity(R.layout.activity_main)
-
public class MainActivity extends Activity {
-
}
可以看到,我们没有了onCreate方法,也没有了setContentView方法,依然能够运行出界面。
[email protected](R.id.xxx)
作用:相当于findViewById(R.id.xxx)。
在布局文件中定义一个按钮:
-
<?xml version="1.0" encoding="utf-8"?>
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:fitsSystemWindows="true"
-
>
-
<Button
-
android:id="@+id/btn"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content" />
-
</LinearLayout>
在Activity中通过注解声明该按钮,注意按钮要声明为public不能为private:
-
@EActivity(R.layout.activity_main)
-
public class MainActivity extends Activity {
-
@ViewById(R.id.btn)
-
public Button btn;
-
}
[email protected](R.id.xxx)
作用:为该按钮注册监听事件,表明该方法为该按钮的点击事件方法
-
@EActivity(R.layout.activity_main)
-
public class MainActivity extends Activity {
-
@ViewById(R.id.btn)
-
public Button btn;
-
@Click(R.id.btn)
-
void myClick(){
-
Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();
-
}
-
}
还可以对多个button进行注解,再通过switch进行判断:
-
@Click({R.id.button1,R.id.button2,R.id.button3})
-
void buttonClicked(Button btn){
-
switch(btn.getId()){
-
case R.id.button1:
-
//这里实现button1的点击事件
-
break;
-
...
-
}
-
}
[email protected]
作用:只有所有View都注入完毕之后,才会执行该注解的方法,一般进行一些初始化的操作
-
@EActivity(R.layout.activity_main)
-
public class MainActivity extends Activity {
-
@ViewById(R.id.btn)
-
public Button btn;
-
@Click(R.id.btn)
-
void myClick(){
-
Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();
-
}
-
@AfterViews
-
void init(){
-
btn.setText("按钮注入完毕后才执行此方法");
-
}
-
}
[email protected](String params)
作用:获得其它地方跳转过来时所传递过来的数据,括号中的字符串即为传递过来的数据的key
如下例子:
MainActivity中:
-
Intent intent = new Intent(MainActivity.this,SecondActivity_.class);
-
Bundle bundle = new Bundle();
-
bundle.putString("hello","hello");
-
intent.putExtras(bundle);
-
startActivity(intent);
SecondActivity:
-
@EActivity(R.layout.activity_second)
-
public class SecondActivity extends Activity{
-
@Extra("hello")
-
String params;
-
@AfterViews
-
void init(){
-
Toast.makeText(this,params,Toast.LENGTH_SHORT).show();
-
}
-
}
跳转的时候注意加下划线:
-
Intent intent = new Intent(MainActivity.this,SecondActivity_.class);
-
startActivity(intent);
[email protected]和@UiThread
@Background作用:相当于用来进行后台操作的方法(比如请求网络资源,操作文件等等),被Backgroung注解的方法里面不能进行任何UI操作,否则会报错。
-
@Background
-
void httprequest(){
-
//进行http操作
-
//操作数据库
-
//操作文件
-
}
@UiThread作用:相当于用来进行UI控件的操作,被UiThread注解的方法里面不能进行任何后台操作,否则会报错
-
@UiThread
-
void runinUI(){
-
Toast.makeText(this,"AndroidAnnotations",Toast.LENGTH_SHORT).show();
-
}
如何将两者结合使用:
-
@AfterViews
-
void init(){
-
httprequest();
-
}
-
@Background
-
void httprequest(){
-
//进行http操作
-
//操作数据库
-
//操作文件
-
//执行完后台操作之后再执行UI操作
-
runinUI();
-
}
-
@UiThread
-
void runinUI(){
-
Toast.makeText(this,"UiThread",Toast.LENGTH_SHORT).show();
-
}
[email protected]
作用:用来标识该类是一个Fragment,并对应哪个布局文件
-
@EFragment(R.layout.layout_myfragment)
-
public class MyFragment extends Fragment{
-
}
[email protected]
作用:用来给当前Fragment接收别的地方传来的数据
MainActivity中:
-
@EActivity(R.layout.activity_main)
-
public class MainActivity extends Activity {
-
@ViewById(R.id.btn)
-
public Button btn;
-
@Click(R.id.btn)
-
void myClick(){
-
MyFragment fragment = new MyFragment_();
-
Bundle bundle = new Bundle();
-
bundle.putString("hello","...");
-
//设置传递的参数
-
fragment.setArguments(bundle);
-
FragmentTransaction transaction = getFragmentManager().beginTransaction();
-
transaction.add(R.id.content, fragment);
-
transaction.commit();
-
}
-
}
activity_main.xml:
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:orientation="vertical"
-
>
-
<Button
-
android:id="@+id/btn"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content" />
-
<FrameLayout
-
android:id="@+id/content"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content">
-
</FrameLayout>
-
</LinearLayout>
MyFragment类:
-
@EFragment(R.layout.layout_myfragment)
-
public class MyFragment extends Fragment{
-
@FragmentArg("hello")
-
String params;
-
@AfterViews
-
void init(){
-
Toast.makeText(getActivity(), params, Toast.LENGTH_SHORT).show();
-
}
-
}
layout_myfragment.xml:
-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-
android:orientation="vertical"
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
>
-
</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,同时另一方面又提高了代码的简洁度和可读性,所以相信其运用到实际开发中是非常有帮助的。