Android注解框架androidannotations简介

     写程序的目的当然是为了给用户提供产品及服务,写程序是辛苦的,有时要加班加点,要抓破头皮等。在写程序的过程中,我们应该尽量让开发工作变得更轻松,更有味。我们能做的每一件事就是尽量减少代码量,不要重复造轮子。Android开源框架androidannotations(我简称AA框架)的目的正是于此,当然代码量减少了,不仅仅可以让工作更轻松,让读代码的人更轻松,维护代码更爽。正在开发的项目中,我们是用到了androidannotations注解框架,所以简单地讲讲:

   androidannotations的官方网址:http://androidannotations.org/及github网址:https://github.com/excilys/androidannotations/wiki,两网址详细介绍了框架的优势及使用,框架的搭建工作可以参考:http://blog.csdn.net/limb99/article/details/9067827。下面用两个简单的Activity(用的同一个布局文件,就简单的6个控件)来对比下代码量:

-------------无注解框架的写法-----------------

   public class TestActivity extends Activity {
private TextView textView01;
private TextView textView02;
private TextView textView03;
private TextView textView04;
private TextView textView05;
private Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.textView01 = (TextView) findViewById(R.id.textView01);
this.textView02 = (TextView) findViewById(R.id.textView02);
this.textView03 = (TextView) findViewById(R.id.textView03);
this.textView04 = (TextView) findViewById(R.id.textView04);
this.textView05 = (TextView) findViewById(R.id.textView05);
this.button = (Button) findViewById(R.id.button);
this.button.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// 实现点击事件
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setViewData();
}
private void setViewData() {
this.textView01.setText("无AA框架测试1");
this.textView02.setText("无AA框架测试2");
this.textView03.setText("无AA框架测试3");
this.textView04.setText("无AA框架测试4");
this.textView05.setText("无AA框架测试5");
}
}

---------用了androidannotations注解框架写法----------------------

@EActivity(R.layout.activity_main)
public class AATestActivity extends Activity {
@ViewById(R.id.textView01)
TextView textView01;
@ViewById(R.id.textView02)
TextView textView02;
@ViewById(R.id.textView03)

TextView textView03;
@ViewById(R.id.textView04)
TextView textView04;
@ViewById(R.id.textView05)
TextView textView05;
@ViewById(R.id.button)
Button button;


@Click(R.id.button)
void btnClick() {
// 实现点击事件
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
setViewData();
}


private void setViewData() {
this.textView01.setText("无AA框架测试1");
this.textView02.setText("无AA框架测试2");
this.textView03.setText("无AA框架测试3");
this.textView04.setText("无AA框架测试4");
this.textView05.setText("无AA框架测试5");
}
}

  相比之下,用了注解写法,就不用重复写findViewById()及写onclick()事件等。这两个类的代码量看不去区别不明显,主要控件少,而且实现功能少,当我们布局页面很复杂,要实现很多监听时,用注解方法的优势就很明显。注解方法很多,可以参考http://doc.okbase.net/rain_butterfly/archive/95335.html,使用极为方便,而且经过简单测试,注解框架对程序的性能影响非常小。

    不过在使用的时候,个人发现了几个感觉不太好的地方: 

  1,我们写在Activity中的控件如Button,不能写成私有的private Button btn。

   2,另外在AndroidManifest.xml清单文件中要把对应的Acitivity(如AATestActivity )写成AATestActivity _才可以。

   3,调试程序可能有些不方便。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。