让我们来看看各种RoboGuice 库的使用方法。
使用RoboGuice库 :
控件注入:用@InjectViews方法初始化控件,例如:@InjectView(R.id.textview1)TextView textView1。
资源注入:用@InjectResources方法初始化资源,例如:@InjectResource(R.string.app_name)String name。
系统服务注入:用@Inject方法初始化并获取系统服务,例如:@Inject LayoutInflater inflater。
POJO对象注入:用@Inject方法注入并初始化POJO对象,例如:@Inject Foo foo。
安装
要使用RoboGuice,你需要下载JAR文件并把他们添加到环境变量中:
http://repo1.maven.org/maven2/org/roboguice/roboguice/2.0/roboguice-2.0.jar
http://repo1.maven.org/maven2/com/google/inject/guice/3.0/guice-3.0-no_aop.jar
http://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar
我们来看看一个简单的一般事件代码:
public class TestActivity extends Activity{
TextView textView1;
TextView textView2;
ImageView imageView1;
String name;
Drawable icLauncher;
LocationManager locManager;
LayoutInflater inflater;
NotificationManager notifyManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_test);
textView1 = (TextView) findViewById(R.id.textView1);
textView2 = (TextView) findViewById(R.id.textView2);
imageView1 = (ImageView) findViewById(R.id.imageView1);
name = getString(R.string.app_name);
icLauncher = getResources().getDrawable(R.id.ic_launcher);
locManager = (LocationManager) getSystemService(Activity.LOCATION_SERVICE);
inflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
notifyManager = (NotificationManager) getSystemService(Activity.NOTIFICATION_SERVICE);
textView1.setText("Hello World! RoboGuice demo");
}
}
再看看使用RoboGuice精简代码后神奇之处。
使用RoboGuice
你先要继承RoboActivity或者RoboFragment,才能使用RoboGuice的依赖注入功能。
public class TestActivity extends RoboActivity{
@InjectView(R.id.textView1) TextView textView1;
@InjectView(R.id.textView2) TextView textView2;
@InjectView(R.id.imageView1) ImageView imageView1;
@InjectResource(R.string.app_name) String name;
@InjectResource(R.drawable.ic_launcher) Drawable icLauncher;
@Inject LocationManager locManager;
@Inject LayoutInflater inflater;
@Inject NotificationManager notifyManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_test);
textView1.setText(name);
}
这么一对比,我想你肯定明白了为什么要使用RoboGuice?再来看看有哪些好处:
|
使用RoboGuice的好处
不需要初始化控件,如有需要就用@InjectViews。
不需要初始化系统服务,如有需要就用@Inject。
不需要初始化像Drawable,string以及其他的资源,如有需要就用@InjectResource。
以上实践能帮助你精简代码。
越少的代码,越少的问题和bugs。
少量的代码让Android开发人员省力同时,也让他们能更专注于实际的业务逻辑。
RoboGuice和ActionBarSherlock
正如我前面提到的,你得在RoboActivity和RoboFragment中继承其中一个才能在Activity事件或Fragment中使用RoboGuice。但是如果你已经在项目中使用了ActionBarSherlock去编译呢?那问题就在于,你已经继承了SherlockActivity或SherlockFragmentActivity中的一个。现在问题是,你不能同时使用RoboGuice和ActionBarSherlock。
解决方法是,为Activities和Fragments定义一个基类。然后你就能同时使用RoboGuice和ActionBarSherlock了。
你可以在这里下载一些基类:
https://github.com/rtyley/roboguice-sherlock 或者下载JAR包也是一样:RoboGuice+Sherlock.jar,你可以任选一个添加到你的项目。
在Android应用程序中,我想我已经做了所有关于RoboGuice用法及好处的研究。如过有什么遗漏,请联系我。在接下来的文章,我会研究其他的函数库好让你成为一个既能偷懒又高效的Android开发人员。
原文:http://mjava.org/1506.shtml