Android依赖注入:Google Guice on Android的使用及相关资源
16lz
2022-01-01
本文转自:http://blog.csdn.net/sangming/article/details/8878104
RoboGuice 使用谷歌自己的Guice库,给Android带来了简单和易用的依赖注入。如果你使用过Spring或Guice的话,你可能已经知道这种编程方式是多么的便捷。
RoboGuice 允许使用annotation 的方式来描述id于View之间的关系,其余的工作由roboGuice库来完成。比如:
[java] view plain copy- classAndroidWayextendsActivity{
- TextViewname;
- ImageViewthumbnail;
- LocationManagerloc;
- Drawableicon;
- StringmyName;
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- name=(TextView)findViewById(R.id.name);
- thumbnail=(ImageView)findViewById(R.id.thumbnail);
- loc=(LocationManager)getSystemService(Activity.LOCATION_SERVICE);
- icon=getResources().getDrawable(R.drawable.icon);
- myName=getString(R.string.app_name);
- name.setText("Hello,"+myName);
- }
- }
如果使用roboguice 来写:
- classRoboWayextendsRoboActivity{
- @InjectView(R.id.name)TextViewname;
- @InjectView(R.id.thumbnail)ImageViewthumbnail;
- @InjectResource(R.drawable.icon)Drawableicon;
- @InjectResource(R.string.app_name)StringmyName;
- @InjectLocationManagerloc;
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- name.setText("Hello,"+myName);
- }
- }
注意:activity必须继承自RoboActivity
收集到的一些资料:github有资料和源码https://github.com/roboguice/roboguice
中文的资料:http://daimajishu.iteye.com/blog/1610501
更多相关文章
- Android中应用界面主题Theme使用方法和页面定时跳转应用
- 关于android中使用new Message的内存泄露问题
- Linux 使用 Nexus3.x 为 Android(安卓)搭建私服
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Fedora镜像
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像