用AndroidAnnotations写android

使用AndroidAnnotations框架写Android代码,减少代码量。清晰易懂。
更多看http://androidannotations.org/或者https://github.com/excilys/androidannotations/wiki
参考此篇文章框架的部署:http://blog.csdn.net/limb99/article/details/9067827

点击(此处)折叠或打开

  1. package com.googlecode.androidannotations.helloworldeclipse;
  2. import java.util.Date;
  3. import java.util.concurrent.TimeUnit;
  4. import android.app.Activity;
  5. import android.app.Notification;
  6. import android.app.NotificationManager;
  7. import android.app.PendingIntent;
  8. import android.content.Intent;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.os.Bundle;
  11. import android.util.Log;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.view.Window;
  15. import android.view.WindowManager;
  16. import android.widget.EditText;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19. import com.googlecode.androidannotations.annotations.Background;
  20. import com.googlecode.androidannotations.annotations.Click;
  21. import com.googlecode.androidannotations.annotations.EActivity;
  22. import com.googlecode.androidannotations.annotations.LongClick;
  23. import com.googlecode.androidannotations.annotations.SystemService;
  24. import com.googlecode.androidannotations.annotations.Touch;
  25. import com.googlecode.androidannotations.annotations.Transactional;
  26. import com.googlecode.androidannotations.annotations.UiThread;
  27. import com.googlecode.androidannotations.annotations.ViewById;
  28. import com.googlecode.androidannotations.annotations.res.BooleanRes;
  29. import com.googlecode.androidannotations.annotations.res.ColorRes;
  30. import com.googlecode.androidannotations.annotations.res.StringRes;
  31. @EActivity(R.layout.my_activity) //布局文件在这里声明,不用在setContentView
  32. public class MyActivity extends Activity {
  33.     @ViewById //控件这样标注,由于是IOC模式,因此不需要自己实例化
  34.     EditText myEditText;
  35.     @ViewById(R.id.myTextView) //提供id来生成控件,如果不指定ID,默认以控件名进行查找,如上面的myEditText
  36.     TextView textView;
  37.     @StringRes(R.string.hello) //资源
  38.     String helloFormat;
  39.     @ColorRes
  40.     int androidColor;
  41.     @BooleanRes
  42.     boolean someBoolean;
  43.     @SystemService
  44.     NotificationManager notificationManager;
  45.     @SystemService
  46.     WindowManager windowManager;
  47.     /**
  48.      * AndroidAnnotations gracefully handles support for onBackPressed, whether
  49.      * you use ECLAIR (2.0), or pre ECLAIR android version.
  50.      */
  51.     public void onBackPressed() {
  52.         Toast.makeText(this, "Back key pressed!", Toast.LENGTH_SHORT).show();
  53.     }
  54.     
  55.     @Override
  56.     protected void onCreate(Bundle savedInstanceState) {
  57.         super.onCreate(savedInstanceState);
  58.         // windowManager should not be null
  59.         windowManager.getDefaultDisplay();
  60.         requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  61.     }
  62.     @Click //事件控制,可以以按钮的id作为方法名,同时支持的事件还有onLongClick,onTextChange等
  63.     void myButtonClicked() {
  64.         String name = myEditText.getText().toString();
  65.         setProgressBarIndeterminateVisibility(true);
  66.         someBackgroundWork(name, 5);
  67.     }
  68.     @Background //开启新线程后台运行,注意不要引用UI控件,而且返回值类型一定是void
  69.     void someBackgroundWork(String name, long timeToDoSomeLongComputation) {
  70.         try {
  71.             TimeUnit.SECONDS.sleep(timeToDoSomeLongComputation);
  72.         } catch (InterruptedException e) {
  73.         }
  74.         String message = String.format(helloFormat, name);
  75.         updateUi(message, androidColor);
  76.         showNotificationsDelayed();
  77.     }
  78.     @UiThread //UI线程
  79.     void updateUi(String message, int color) {
  80.         setProgressBarIndeterminateVisibility(false);
  81.         textView.setText(message);
  82.         textView.setTextColor(color);
  83.     }
  84.     @UiThread(delay=2000) //可以设置延时时间,以毫秒为单位
  85.     void showNotificationsDelayed() {
  86.         Notification notification = new Notification(R.drawable.icon, "Hello !", 0);
  87.         PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(), 0);
  88.         notification.setLatestEventInfo(getApplicationContext(), "My notification", "Hello World!", contentIntent);
  89.         notificationManager.notify(1, notification);
  90.     }
  91.     @LongClick
  92.     void startExtraActivity() {
  93.         Intent intent = new Intent(this, ActivityWithExtra_.class);
  94.         intent.putExtra(ActivityWithExtra.MY_DATE_EXTRA, new Date());
  95.         intent.putExtra(ActivityWithExtra.MY_STRING_EXTRA, "hello !");
  96.         intent.putExtra(ActivityWithExtra.MY_INT_EXTRA, 42);
  97.         startActivity(intent);
  98.     }
  99.     @Click
  100.     void startListActivity(View v) {
  101.         startActivity(new Intent(this, MyListActivity_.class));
  102.     }
  103.     @Touch
  104.     void myTextView(MotionEvent event) {
  105.         Log.d("MyActivity", "myTextView was touched!");
  106.     }
  107.     @Transactional
  108.     int transactionalMethod(SQLiteDatabase db, int someParam) {
  109.         return 42;
  110.     }
  111. }
 
posted @ 2015-03-15 21:35  小z亦小  阅读(563)  评论(0编辑  收藏  举报