zoukankan      html  css  js  c++  java
  • Android依赖注入:Google Guice on Android的使用及相关资源

    本文转自:http://blog.csdn.net/sangming/article/details/8878104

    RoboGuice 使用谷歌自己的Guice库,给Android带来了简单和易用的依赖注入。如果你使用过Spring或Guice的话,你可能已经知道这种编程方式是多么的便捷。

    RoboGuice 允许使用annotation 的方式来描述id于View之间的关系,其余的工作由roboGuice库来完成。比如:

    1. class AndroidWay extends Activity {  
    2.  TextView name;  
    3.  ImageView thumbnail;  
    4.  LocationManager loc;  
    5.  Drawable icon;  
    6.  String myName;  
    7.   
    8.  public void onCreate(Bundle savedInstanceState) {  
    9.  super.onCreate(savedInstanceState);  
    10.  setContentView(R.layout.main);  
    11.  name      = (TextView) findViewById(R.id.name);  
    12.  thumbnail = (ImageView) findViewById(R.id.thumbnail);  
    13.  loc       = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);  
    14.  icon      = getResources().getDrawable(R.drawable.icon);  
    15.  myName    = getString(R.string.app_name);  
    16.  name.setText( "Hello, " + myName );  
    17.  }  
    18. }  

    如果使用roboguice 来写:

    1. class RoboWay extends RoboActivity {  
    2.  @InjectView(R.id.name)             TextView name;  
    3.  @InjectView(R.id.thumbnail)        ImageView thumbnail;  
    4.  @InjectResource(R.drawable.icon)   Drawable icon;  
    5.  @InjectResource(R.string.app_name) String myName;  
    6.  @Inject                            LocationManager loc;  
    7.   
    8.  public void onCreate(Bundle savedInstanceState) {  
    9.  super.onCreate(savedInstanceState);  
    10.  setContentView(R.layout.main);  
    11.  name.setText( "Hello, " + myName );  
    12.  }  
    13. }  


    只需使用@InjectView 来描述 view 和Id之间的关系,RoboGuice 自动完成余下的工作,代码简洁易读。


    注意:activity必须继承自RoboActivity


    收集到的一些资料:github有资料和源码https://github.com/roboguice/roboguice

    中文的资料:http://daimajishu.iteye.com/blog/1610501

  • 相关阅读:
    视图
    Mysql事务
    子查询
    Mysql连表查询
    Mysql增删改查
    Mysql数据类型
    EntityFramwork 查询
    Git
    EntityFramework走马观花之CRUD(下)
    EntityFramework走马观花之CRUD(中)
  • 原文地址:https://www.cnblogs.com/androidsuperman/p/3472716.html
Copyright © 2011-2022 走看看