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()
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 . . .]

Paste_Image.png
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(){}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容