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”。