Wirelessly tutorial (2): The image inject in @InjectView

Source: Internet
Author: User

1  PackageCom.example.ele_me.util;2 3 Importjava.lang.annotation.Annotation;4 ImportJava.lang.reflect.Field;5 6 Importandroid.app.Activity;7 8 /**9 * Very Lightweight form of injection, inspired by roboguice, for injecting common UI elements.Ten * <p> One * Usage is very simple. In your Activity, define some fields as follows: A  * - * <pre class= "code" > - * @InjectView (R.id.fetch_button) the * Private Button Mfetchbutton; - * @InjectView (R.id.submit_button) - * Private Button Msubmitbutton; - * @InjectView (R.id.main_view) + * Private TextView Mtextview; - * </pre> + * <p> A * Then, inside your Activity's OnCreate () method, perform the injection like this: at  * - * <pre class= "code" > - * Setcontentview (r.layout.main_layout); - * Injector.get (this). inject (); - * </pre> - * <p> in * See the {@link#inject ()} method for full details about how it works. Note that the fields is - * fetched and assigned at the time-call {@link#inject ()}, consequently you should does this to * until after you ' ve called the Setcontentview () method. +  */ -  Public Final classInjector { thePrivate FinalActivity mactivity; *  $PrivateInjector (activity activity) {Panax NotoginsengMactivity =activity; -     } the //macticity itself has a unique variable and is gifted to class Injector, but we can also change the activity to alter the mactivity.  +/** A * Gets an {@linkInjector} Capable of injecting fields for the given Activity. the      */ + Public StaticInjector Get (activity activity) { -return NewInjector (activity); $     } $  -/** - * Injects all fields, that is marked with the {@linkInjectview} annotation. the * <p> - * For each field marked with the Injectview annotationWuyi      * {@linkActivity#findviewbyid (int)} 'll be made, passing in the resource ID stored in the the * Value () method of the Injectview annotation as the int parameter, and the result of this call - * 'll is assigned to the field. Wu      * -      * @throwsillegalstateexception If injection fails, common causes being so you had used an About * Invalid ID value, or haven ' t called Setcontentview () on your Activity. $      */ - Public voidinject () - //inject waits to be called by another Java file.  -  { A for(Field field:mActivity.getClass (). Getdeclaredfields ()) + //field is an image type that acts as a findviewbyid to capture its image. The following for loop is used to capture the equation that is needed for the image.  the { - for(Annotation annotation:field.getAnnotations ()) { $if(Annotation.annotationtype (). Equals (Injectview.class)) { the Try { theclass<?> FieldType =Field.gettype (); theintIdvalue = Injectview.class. Cast (annotation). value (); theField.setaccessible (true); -Object Injectedvalue =Fieldtype.cast (Mactivity.findviewbyid (idvalue)); inif(Injectedvalue = =NULL) { theThrow NewIllegalStateException ("Findviewbyid (" +Idvalue the+ ") gave null for" + AboutField + ", can ' t inject"); the                         } the Field.set (mactivity, injectedvalue); theField.setaccessible (false); +}Catch(illegalaccessexception e) { -Throw NewIllegalStateException (e); the                     }Bayi                 } the             } the         } -     } -}

This is a required code. We have to reopen a Java file to let Injectview run, and Injectview itself does not look for the right image alone, but through Injectview Inject () to calculate and look for the image.

Wirelessly tutorial (2): The image inject in @InjectView

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.