DHROID-IOC Foundation (@Inject *)

Source: Internet
Author: User
Tags file copy

1 The IOC is control reversal.
Control inversion (inversion of the English abbreviation for IOC) is an important object-oriented programming principle to reduce the coupling problem of computer programs, and is also the core of the lightweight spring framework.
Control inversion There is also a name called Dependency injection (Dependency injection)
Dhroid's IOC not only realizes the injection of views and resources, but also makes great efforts for the injection of objects and interfaces.

The simplest and best implementation of the IOC is the injection of view resources, in fact the dhroid IOC core is an object (interface) injection
The following is also a simple view injection from the beginning to the complex interface object injection
1.1 Injection View

 Public class extends baseactivity{@InjectView (id=r.id.text) TextView textv; @Overrideprotectedvoid onCreate (Bundle savedinstancestate) {superonCreate (savedinstancestate); Setcontentview ( r.layout.ioc_test_activity); if (textv==null) {log.e ("dhroid", "Textv not empty, I will not print"); }}}

Above is an example of a view injection
TEXTV has been assigned after Contentview (layout).

1.1 Injection View
The annotation injected into the view is @InjectView

@InjectView, the parameter ID for the view lookup, equivalent to Findviewbyid (r.id.xxx) @InjectView parameter property layout can be injected into a layout, Equivalent to Layoutinflater.from (this null), such as @injectview (layout=//  Injection layout file @InjectView (layout=r.layout.ioc_head) viewgroup headv; // View the @InjectView in other views (id=r.id.intext,inview= "Headv") TextView Childtextv;

Indicates that CHILDTEXTV is a child view of HEADV
Equivalent

headv= (ViewGroup) layoutinflater.from (thisnull); Childtextv=headv.findviewbyid ( R.id.intext);

1.1.1 Adding an event to a view

The following code adds a click event

@InjectView (id=r.id.assertfile,click= "Toinstal") View instalapkv; Public voidToinstal (View v) {LOG.V ("Dhroid", "instalapk I will be executed after clicking");} The event also has ItemClick and Itemlongclick@injectview (ID=r.id.listview,itemclick= "Toeditstudent", itemlongclick= "Todeletestudent") ListView ListView; Public voidToeditstudent (adapterview<?> Parent, view view,intPositionLongID) {LOG.V ("Dhroid", "ItemClick event");} Public voidTodeletestudent (adapterview<?> Parent, view view,Final intPositionLongID) {LOG.V ("Dhroid", "Itemlongclick event");}


1.2 Inject Resources
Using Annotations @injectresource
Resources can be injected into drawable,string,color,dimen

The following code is equivalent to Getresources (). getString (R.string.app_name)

// injects a string @InjectResource (string=R.string.app_name) strings appname;


The following code is equivalent to Getresources (). getdrawable (R.drawable.ic_launcher)

@InjectResource (drawable=r.drawable.ic_launcher) drawable Icdraw;color, and dimen the same //  This cannot be an int because an int has a default value of 0 The property that has a value is ignored, preventing repeated injection @InjectResource (color=r.color.link) Integer Colorlink; // inject picture @InjectResource (drawable=r.drawable.ic_launcher) drawable Icdraw;

1.3 Injection Extra
Data transfer between pages can also be injected into the
The following code is equivalent to Getintent (). Getstringextra ("str");

// accepts the incoming string @InjectExtra (name= "str", def= "default value") string extra;

Integer,long,float,double,boolean (Special Jsonobject,jsonarray, other bean objects can be passed)
Special instructions are needed here. Int,long,float,double cannot be used because they have default values, and when injected, they are found to have filtered values.
If you have a default value, write to the Def attribute,
Special Jsonobject,jsonarray and Bean objects are the result of special casts, and incoming objects are also string types

Incoming It.putextra ("Jo", "{name: ' I am a JSON string '}"); It.putextra ("Array", "[{name: ' I am the JSON array '}]"); It.putextra ("Bean", "{name: ' I am a JSON string '}"); @injectextra (name= "Jo") jsonobject extrajo;@ Injectextra (Name= "Array") Jsonarray Extraarray; @InjectExtra (name= "Bean") User Extrauser;


1.4 Inject assert
@InjectAssert is primarily used to inject files in an assert.

The following code can inject the text content of Testtext.json below the Assert folder

@InjectAssert (path= "Testtext.json") String Testassert;

If your text content is originally JSON can be written as

@InjectAssert (path= "Testtext.json") jsonobject Jo;


We can also inject files like

@InjectAssert (path= "anzhi.apk") File apkfile;

Because the assert itself does not support the file read and write, so in fact, the first copy of the file and then assign value, the file copy is asynchronous when using the need to pay attention to whether the file is copied to complete,

Can be injected once using the previous page (the file will only be copied once)

Fragment use if you are not inheriting from baseactivity, you need to call Injectutil.inject (this) after Setcontentview ();
If you use in fragment, you need to call Injectutil.inject (this) in the Onactivitycreated method;
Also can be used in custom view, also Injectutil.inject (this);
Actually, Baseactivity is very simple inside.


About class inheritance issues
When inheriting a class
Only common attributes in the parent class, that is, the properties of public can be injected

DHROID-IOC Foundation (@Inject *)

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.