AndroidAnnotations简单示例
2015-11-18 17:19
148 查看
@EActivity(R.layout.activity_main) public class MainActivity extends Activity { @ViewById(R.id.textView) TextView textView; @ViewById //不指定ID,默认以控件名进行查找 Button button; @StringRes //获取资源文件值 String hello_world; @SystemService //实例化系统服务 NotificationManager notificationManager; @SystemService WindowManager windowManager; DisplayMetrics dm; @Click//事件控制,可以以按钮的id作为方法名 public void buttonClicked() { textView.setText("值变了" + hello_world); Toast.makeText(MainActivity.this, "hello ", Toast.LENGTH_LONG).show(); someBackgroundWork("丽丽", 5); } @AfterViews //初始化控件值 public void init() { textView.setText("初始值 " + dm.widthPixels + " X " + dm.heightPixels); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); dm = new DisplayMetrics(); windowManager.getDefaultDisplay().getMetrics(dm); } @Background //开启新线程后台运行,而且返回值类型一定是void void someBackgroundWork(String name, long timeToDoSomeLongComputation) { SystemClock.sleep(timeToDoSomeLongComputation); updateUi("hello " + name, Color.RED); showNotificationsDelayed(); } @UiThread //UI线程 void updateUi(String message, int color) { textView.setText(message); textView.setTextColor(color); } @UiThread(delay = 2000) //可以设置延时时间,以毫秒为单位 void showNotificationsDelayed() { Notification notification = new Notification(R.mipmap.ic_launcher, "Hello !", 0); PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0); notification.setLatestEventInfo(getApplicationContext(), "My notification", "Hello World!", contentIntent); notificationManager.notify(1, notification); } }
注意:
使用AndroidAnnotations,编译的时候会生成一个子类,这个子类的名称就是在原来的类之后加了一个下划线“_”,比如这个例子产生的子类名称为“MainActivity_”,这就需要你在注册这个Activity的时候,在
AndroidManifest.xml中将 MainActivity 改为 MainActivity_ ,使用的时候也是使用MainActivity_来表示此类,如:
startActivity(new Intent(this,MainActivity_.class));
相关文章推荐
- Android Context完全解析,你所不知道的Context的各种细节
- android中之断点续传
- Android自定义手势识别
- 【Android学习笔记】Broadcast receiver
- android进行录音功能并保存播放
- Android控件之Service AIDL实现机制demo
- Android 如果布局中有ScrollView和Fragment或者带有滚动条的布局进行嵌套,布局加载完成页面无法定位到顶部的情况
- WINDOWS7环境下android studio、NDK配置补充
- Android过渡动画之进入退出
- 关于图片加载优化的小技巧
- android在java代码中动态添加组件及相关布局方法(LayoutParams)
- Android中的Service的用法 开启服务,下一篇讲绑定服务
- Android 音效流程分析
- Android开发之Android Material Design Toolbar自定义随笔
- Android Service小记
- Android Studio Prettify 插件
- android获取view宽高的几种方法,解决获取高宽为0问题
- Android fragment源码全解析
- AndroidSwipeLayout源码
- Android下开机自启动C程序