V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
obiwong
V2EX  ›  Android

变魔术的RoboGuice

  •  
  •   obiwong · 2011-03-31 20:04:53 +08:00 · 10615 次点击
    这是一个创建于 5020 天前的主题,其中的信息可能已经有所发展或是发生改变。
    site: http://code.google.com/p/roboguice/

    RoboGuice is a framework that brings the simplicity and ease of Dependency Injection to Android, using Google's own Guice library.

    打个比方,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 );
    }
    }

    使用后:
    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 );
    }
    }

    PS: 如果无法访问官网http://code.google.com/p/roboguice/, 可以试试maven.

    PS2: 如果对Google Guice感兴趣,apress出版了一本书 “Google Guice”。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2542 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 20ms · UTC 15:07 · PVG 23:07 · LAX 07:07 · JFK 10:07
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.