AndroidAnnotations这个开源项目为android程序的编写提供了一个新的思路,他们自己称之为框架,我觉得很像struts2的注解方式,下面是这个项目在github上面的地址
https://github.com/excilys/androidannotations
下面的代码是一个例子
这个项目的好处是使用到了IOC模式,代码量小而且简洁,使程序员更多的关注于业务逻辑而不是页面,而且@Backgroud这个标签下开启的线程在线程池中管理。
类似的项目还有Android Binding,RoboGuice等,有兴趣可以看一下这个文章
CLEAN CODE IN ANDROID APPLICATIONS一个Spring gay写得,很不错哦~
http://blog.springsource.com/2011/08/26/clean-code-with-android/
]]>
- 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;
- }
- }
评论区