让咱们来看看各类RoboGuice 库的使用方法。html
使用RoboGuice库 :java
控件注入:用@InjectViews方法初始化控件,例如:@InjectView(R.id.textview1)TextView textView1。git
资源注入:用@InjectResources方法初始化资源,例如:@InjectResource(R.string.app_name)String name。github
系统服务注入:用@Inject方法初始化并获取系统服务,例如:@Inject LayoutInflater inflater。app
POJO对象注入:用@Inject方法注入并初始化POJO对象,例如:@Inject Foo foo。maven
安装ide
要使用RoboGuice,你须要下载JAR文件并把他们添加到环境变量中:函数
http://repo1.maven.org/maven2/org/roboguice/roboguice/2.0/roboguice-2.0.jarui
http://repo1.maven.org/maven2/com/google/inject/guice/3.0/guice-3.0-no_aop.jargoogle
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