Android Annotations 介绍
来源:互联网 发布:iphone6s连不上网络 编辑:程序博客网 时间:2024/05/18 17:45
开发过程中看到别人使用了Android Annotations的代码,该代码是一个第三方框架,通过注释来开发应用。使用Android Annotations能够大大减少代码量。
请看下面的Java代码
- package com.googlecode.androidannotations.helloworldeclipse;
- importjava.util.Date;
- importjava.util.concurrent.TimeUnit;
- import android.app.Activity;
- import android.app.Notification;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.content.Intent;
- import android.database.sqlite.SQLiteDatabase;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.MotionEvent;
- import android.view.View;
- import android.view.Window;
- import android.view.WindowManager;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.googlecode.androidannotations.annotations.Background;
- import com.googlecode.androidannotations.annotations.Click;
- import com.googlecode.androidannotations.annotations.EActivity;
- import com.googlecode.androidannotations.annotations.LongClick;
- import com.googlecode.androidannotations.annotations.SystemService;
- import com.googlecode.androidannotations.annotations.Touch;
- import com.googlecode.androidannotations.annotations.Transactional;
- import com.googlecode.androidannotations.annotations.UiThread;
- import com.googlecode.androidannotations.annotations.ViewById;
- import com.googlecode.androidannotations.annotations.res.BooleanRes;
- import com.googlecode.androidannotations.annotations.res.ColorRes;
- import com.googlecode.androidannotations.annotations.res.StringRes;
- @EActivity(R.layout.my_activity)//布局文件在这里声明,不用在setContentView
- publicclass MyActivityextendsActivity{
- @ViewById //控件这样标注,由于是IOC模式,因此不需要自己实例化
- EditText myEditText;
- @ViewById(R.id.myTextView)//提供id来生成控件,如果不指定ID,默认以控件名进行查找,如上面的myEditText
- TextView textView;
- @StringRes(R.string.hello)//资源
- String helloFormat;
- @ColorRes
- int androidColor;
- @BooleanRes
- boolean someBoolean;
- @SystemService
- NotificationManager notificationManager;
- @SystemService
- WindowManager windowManager;
- /**
- * AndroidAnnotations gracefully handles support for onBackPressed, whether
- * you use ECLAIR (2.0), or pre ECLAIR android version.
- */
- public void onBackPressed(){
- Toast.makeText(this,"Back key pressed!", Toast.LENGTH_SHORT).show();
- }
-
- @Override
- protectedvoid onCreate(Bundle savedInstanceState){
- super.onCreate(savedInstanceState);
- // windowManager should not be null
- windowManager.getDefaultDisplay();
- requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
- }
- @Click //事件控制,可以以按钮的id作为方法名,同时支持的事件还有onLongClick,onTextChange等
- void myButtonClicked(){
- Stringname= myEditText.getText().toString();
- setProgressBarIndeterminateVisibility(true);
- someBackgroundWork(name, 5);
- }
- @Background //开启新线程后台运行,注意不要引用UI控件,而且返回值类型一定是void
- void someBackgroundWork(Stringname,long timeToDoSomeLongComputation){
- try {
- TimeUnit.SECONDS.sleep(timeToDoSomeLongComputation);
- } catch (InterruptedException e){
- }
- String message=String.format(helloFormat,name);
- updateUi(message, androidColor);
- showNotificationsDelayed();
- }
- @UiThread //UI线程
- void updateUi(String message,intcolor){
- setProgressBarIndeterminateVisibility(false);
- textView.setText(message);
- textView.setTextColor(color);
- }
- @UiThread(delay=2000)//可以设置延时时间,以毫秒为单位
- void showNotificationsDelayed(){
- Notificationnotification=new Notification(R.drawable.icon,"Hello !", 0);
- PendingIntent contentIntent = PendingIntent.getActivity(this, 0,new Intent(), 0);
- notification.setLatestEventInfo(getApplicationContext(),"My notification","Hello World!", contentIntent);
- notificationManager.notify(1,notification);
- }
- @LongClick
- void startExtraActivity(){
- Intent intent = new Intent(this, ActivityWithExtra_.class);
- intent.putExtra(ActivityWithExtra.MY_DATE_EXTRA,newDate());
- intent.putExtra(ActivityWithExtra.MY_STRING_EXTRA,"hello !");
- intent.putExtra(ActivityWithExtra.MY_INT_EXTRA, 42);
- startActivity(intent);
- }
- @Click
- void startListActivity(View v){
- startActivity(new Intent(this, MyListActivity_.class));
- }
- @Touch
- void myTextView(MotionEventevent){
- Log.d("MyActivity","myTextView was touched!");
- }
- @Transactional
- int transactionalMethod(SQLiteDatabase db,int someParam){
- return 42;
- }
- }
@AfterInject 定义的方法在类的构造方法执行后执行
@AfterTextChange定义的方法在TextView及其子类的Text属性改变后执行
@AfterViews 定义的方法在setContentView后执行
@Background 定义的方法在后台线程执行
@BeforeTextChange 定义的方法在TextView及其子类的Text属性改变前执行
@Click 定义点击监听器
@EActivity 在Activity中启用Annotations
@EProvider 在 ContentProvider中启用Annotations
@EReceive 在BroadcastReceiver中启用Annotations
@EService 在Service中启用Annotations
@EView 在自定义的View的子类中启用Annotations
@Fullscreen 全屏
@NoTitle 无标题栏
Annotation有个问题:不能直接这样定义命名为id:int id=0;如果要这样定义要定义成private或public,否则会报错,要么可以这样定义:int ids=0;总之就是不能直接命名为id,否则会冲突
掌握这些注视对读懂利用该第三方代码开发的代码非常有帮助,同时对利用该代码开发的APK文件反编译的解释能够有更深入的了解。
0 0
- Android Annotations 介绍
- Android Annotations 介绍
- Android Annotations 介绍
- Android Annotations
- Android Annotations
- Android annotations
- android annotations
- struts-annotations-plugin 介绍
- tapestry注释介绍Annotations
- tapestry注释介绍Annotations
- tapestry注释介绍Annotations
- 试用Android Annotations
- Android Annotations浅析
- Android Annotations浅析
- android工具-annotations
- android工具-annotations
- 简单试用Android Annotations
- Android Annotations浅析
- Webservices
- 测试动态显示
- Boost学习笔记——boost的编译
- SOAP
- 将DataTable转化为List<T>
- Android Annotations 介绍
- Ubuntu 下 使用 adb logcat 显示 Android 日志
- 如何使用免费PDF第三方插件从PDF文档中提取文本和图片
- Android - RGB颜色查询对照表
- 冰冻三尺非一日之寒之产品观
- linux常见问题一 /lib/ld-linux.so.2: bad ELF interpreter: 没有那个文件或目录
- android listView 点击无响应的解决方法
- Identifying Read/Write (I/O) Problems
- Openssl建立SSL双向认证连接源码