主页: http://androidannotations.org/


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


配置:
1. 在**project/build.gradle**文件中按下图所示添加代码:

mavenCentral()
classpath
'com.neenbedankt.gradle.plugins:android-apt:1.8'
mavenCentral()
mavenLocal()

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"

* 注意事项:
*
Manifest中注册的activity要在原类名后追加下划线”_”
* 使用注解的控件和方法不能使用private修饰符
*
大型项目并不适用

示例代码:

注意导包导入的是:

import org.androidannotations.annotations......
@Fullscreen //全屏
@WindowFeature(Window.FEATURE_NO_TITLE) //Activity没有标题
@EActivity(R.layout.my_activity) //布局文件在这里声明,不用在setContentView
public class MyActivity extends Activity { @ViewById //初始化控件,不需要自己实例化
EditText myEditText; @ViewById(R.id.myTextView) //提供id来生成控件,如果不指定ID,默认以控件名进行查找,如上面的myEditText
TextView textView; @StringRes(R.string.app_name) //字符串资源
String appName; @ColorRes(R.color.colorAccent) //颜色资源
int androidColor; @BooleanRes
boolean someBoolean; @SystemService //系统服务
NotificationManager notificationManager; @Click //事件控制,可以以按钮的id作为方法名,同时支持的事件还有onLongClick,onTextChange等
void myButtonClicked() {
String name = myEditText.getText().toString();
setProgressBarIndeterminateVisibility(true);
someBackgroundWork(name, 5);
} @ViewById(R.id.textView)
TextView tv; @Click(R.id.button)
void submit() {
tv.setText(appName);
someBackgroundWork();
} @Background//开启新线程后台运行,注意不要引用UI控件,而且返回值类型一定是void
void someBackgroundWork() {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {} updateUi(appName, androidColor);
} @UiThread//UI线程
void updateUi(String message, int color) {
tv.setText(message);
tv.setTextColor(color);
} @LongClick
void startExtraActivity() {
Intent intent = new Intent(this, ActivityWithExtra_.class); intent.putExtra(ActivityWithExtra.MY_DATE_EXTRA, new Date());
intent.putExtra(ActivityWithExtra.MY_STRING_EXTRA, "hello !");
intent.putExtra(ActivityWithExtra.MY_INT_EXTRA, 42); startActivity(intent);
} @Touch
void myTextView(MotionEvent event) {
Log.d("MyActivity", "myTextView was touched!");
}
}

最新文章

  1. 编写高质量代码:改善Java程序的151个建议(第8章:多线程和并发___建议126~128)
  2. 数据量大的数据转换成jason并显示在页面上
  3. java 排序
  4. 高级Javascript调试——console.table()
  5. android开发中scrollview添加自定义view的滑动显示问题
  6. 《深入理解Nginx》阅读与实践(四):简单的HTTP过滤模块
  7. RabbitMQ - 实例操作
  8. poj 2594 传递闭包+最大路径覆盖
  9. document.location的属性
  10. 测试functional的bind以及相关功能
  11. Java通过axis调用.NET WebService
  12. poj 3082多边形相交 'Roid Rage
  13. Python数据结构-序表
  14. Ubuntu发行版升级
  15. js中的匿名函数自执行
  16. python web架构初步认识
  17. Spark MLlib之水塘抽样算法(Reservoir Sampling)
  18. jdbctemplate 批量插入
  19. UI基础:UINavigationController、界面通信
  20. vue.js 源代码学习笔记 ----- decoder

热门文章

  1. 论文笔记:Integrated Object Detection and Tracking with Tracklet-Conditioned Detection
  2. python面向编程:类的组合、封装、property装饰器、多态
  3. Yii2 DatePicker时间选择器的使用
  4. FLUSH TABLES WITH READ LOCK 获取锁的速度
  5. SAP笔记
  6. sql 184. 部门工资最高的员工
  7. 浅入深出Vue:文章列表
  8. NoSQL数据库技术实战-第1章 NoSQL与大数据简介 NoSQL数据库的类型
  9. 代理模式-aop
  10. git报错remote: error: cannot run hooks/post-receive: No such file or directory