开源项目AndroidAnnotations
来源:互联网 发布:mac 微软雅黑字体下载 编辑:程序博客网 时间:2024/06/05 08:02
Android Annotations是一个开源的框架,用于加速 Android应用的开发,可以让你把重点放在功能的实现上,简化了代码,提升了可维护性。
项目地址:https://github.com/excilys/androidannotations
package com.googlecode.androidannotations.helloworldeclipse; import java.util.Date; import java.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 public class MyActivity extends Activity { @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 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // windowManager should not be null windowManager.getDefaultDisplay(); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); } @Click //事件控制,可以以按钮的id作为方法名,同时支持的事件还有onLongClick,onTextChange等 void myButtonClicked() { String name = myEditText.getText().toString(); setProgressBarIndeterminateVisibility(true); someBackgroundWork(name, 5); } @Background //开启新线程后台运行,注意不要引用UI控件,而且返回值类型一定是void void someBackgroundWork(String name, 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, int color) { setProgressBarIndeterminateVisibility(false); textView.setText(message); textView.setTextColor(color); } @UiThread(delay=2000) //可以设置延时时间,以毫秒为单位 void showNotificationsDelayed() { Notification notification = 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, new Date()); 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(MotionEvent event) { Log.d("MyActivity", "myTextView was touched!"); } @Transactional int transactionalMethod(SQLiteDatabase db, int someParam) { return 42; } }
这个项目的好处是使用到了IOC模式,代码量小而且简洁,使程序员更多的关注于业务逻辑而不是页面,而且@Backgroud这个标签下开启的线程在线程池中管理。
类似的项目还有Android Binding,RoboGuice等,有兴趣可以看一下这个文章
CLEAN CODE IN ANDROID APPLICATIONS一个Spring gay写得,很不错哦~
http://blog.springsource.com/2011/08/26/clean-code-with-android/
- 开源项目AndroidAnnotations
- AndroidAnnotations开源框架
- AndroidAnnotations开源框架
- 开源框架 : 注解 androidannotations
- Android开源组件介绍:androidannotations
- Android开源框架(一):AndroidAnnotations
- Android:Android开源框架:AndroidAnnotations
- Android开源框架之androidannotations
- Android开源框架(一):AndroidAnnotations
- Android开源框架:AndroidAnnotations基本介绍
- Android开源框架(一):AndroidAnnotations
- Android开源框架(一):AndroidAnnotations
- 开源框架——AndroidAnnotations
- Android开源框架(一):AndroidAnnotations
- Android开源框架(一):AndroidAnnotations
- AndroidAnnotations
- AndroidAnnotations
- AndroidAnnotations
- 文本编程
- 原型模式 类继承
- Javascript call apply
- CSS Hack
- CSS3 Transition Transform Animat…
- 开源项目AndroidAnnotations
- jQuery选择器 (详解)[转]
- register关键字在c语言和c++中的差异
- Facebook是如何做自动化测试的
- 我的第一个HTML5游戏——打地鼠总结…
- 我的第一个HTML5游戏——打地鼠具体…
- 简单淘宝瀑布流展示效果_代码解说
- Javascript 静态方法和常规方法
- map的一个使用技巧