讓咱們來看看各類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