Android dependency injection function library Roboguice (1)

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.