Android dependency injection function library Roboguice (1)
Roboguice is a dependency injection framework for Android applications. It uses the official Guice library of Google to greatly simplify Android dependency injection. It makes programming easier and more interesting.
To use Roboguice, You need to download several jar files.
When Roboguice is used, the Activity page must inherit the RoboActivity
/*****/Package com. example. roboguicetest; import com. google. inject. inject; import roboguice. activity. roboActivity; import roboguice. inject. injectResource; import roboguice. inject. injectView; import android. OS. bundle; import android. view. layoutInflater; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. toast;/*** must inherit the RoboActivity class RoboAc Tivity is the Activity subclass */public class MainActivity extends RoboActivity {/*** Control Injection (equivalent to Button btnShow1 = (Button) findViewById (R. id. btn_show1);) */@ InjectView (R. id. btn_show1) Button btnShow1; @ InjectView (R. id. btn_show2) Button btnShow2;/*** resource injection */@ InjectResource (R. string. toast) String toast;/*** System Service injection */@ InjectLayoutInflater inflater; @ InjectView view;/*** POJO object injection */@ InjectUserInfo info; @ Overridepr Otected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); btnShow1.setText ("Hello! "); BtnShow2.setText (" OK "); Toast. makeText (this, toast, Toast. LENGTH_LONG ). show (); view = inflater. inflate (R. layout. main, null); info. setName ("xionglh"); info. setPwd ("123"); btnShow1.setOnClickListener (onclick);} private OnClickListener onclick = new OnClickListener () {@ Overridepublic void onClick (View v) {Toast. makeText (MainActivity. this, info. getName () + "--" + info. getPwd (), Toast. LENGTH_LONG ). show ();}};}
Simple instance download