AndroidAnnotations--依赖注入

主页: http://androidannotations.org/

AndroidAnnotations的优点
1.使用依赖注入Views,extras,System Service,resources
2.简化线程模型
3.事件绑定
4.REST Client

使用:
依赖配置比较繁琐,直接上图
1.在project/build.gralde文件中添加如下代码
mavenCentral()

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

mavenCentral()
mavenLocal()
AndroidAnnotations--依赖注入_第1张图片
Paste_Image.png

2.在app/build.gradle文件中添加如下代码

apply plugin: 'android-apt'

apt {
    arguments {
        androidManifestFile variant.outputs[0]?.processResources?.manifestFile
    }
}

apt "org.androidannotations:androidannotations:4.0.0"
compile "org.androidannotations:androidannotations-api:4.0.0"

![Uploading Paste_Image_617775.png . . .]

AndroidAnnotations--依赖注入_第2张图片
Paste_Image.png
AndroidAnnotations--依赖注入_第3张图片
Paste_Image.png

注意事项:清单文件中注册的activity要在原类名之后追加下划线"_",使用注解的控件和方法不能被private修饰符修饰,该框架大型项目并不适用

可在Activity上添加注入代码
@Fullscreen //全屏
@WindowFeature(Window.FEATURE_NO_TITLE)//无标题
@EActivity(R.layout.my_activity) //在这里声明布局文件,不用setContentView()

//初始化控件
@ViewById(R.id.myTextView)
TextView textView
//字符串资源
@StringRes(R.string.app_name)
String appName;
//颜色资源
@ColorRes(R.color.colorAccent)
int androidColor;
//系统服务
@SystemService
NotificationManager notificationManager;
//事件控制,以按钮的id作为方法名
@Click
void myButtonClicked(){}
@Click(R.id.button)
void submit(){}
//开启新线程后台运行,注意不要引用UI控件,并且返回值类型一定是void
@Background
void someBackgroundWork(){}
//UiThread//UI线程
void updateUi(){}

你可能感兴趣的:(AndroidAnnotations--依赖注入)