Android依赖注入:Google Guice on Android
16lz
2021-12-04
RoboGuice 使用谷歌自己的Guice库,给Android带来了简单和易用的依赖注入。如果你使用过Spring或Guice的话,你可能已经知道这种编程方式是多么的便捷。
RoboGuice 允许使用annotation 的方式来描述id于View之间的关系,其余的工作由roboGuice库来完成。比如:
class AndroidWay extends Activity { TextView name; ImageView thumbnail; LocationManager loc; Drawable icon; String myName; public void onCreate(Bundle savedInstanceState) { 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 来写:
class RoboWay extends RoboActivity { @InjectView(R.id.name) TextView name; @InjectView(R.id.thumbnail) ImageView thumbnail; @InjectResource(R.drawable.icon) Drawable icon; @InjectResource(R.string.app_name) String myName; @Inject LocationManager loc; public void onCreate(Bundle savedInstanceState) { 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
更多相关文章
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Fedora镜像
- 箭头函数的基础使用
- NPM 和webpack 的基础使用
- Python list sort方法的具体使用
- 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
- 【阿里云镜像】切换阿里巴巴开源镜像站镜像——Debian镜像
- Android中BindService方式使用的理解
- 自己收藏的Android开发的知识点各种传送门 持续更新中~~
- Android中Shape和selector的使用