Android Annotated IOC framework "Viewinject" (i)

Source: Internet
Author: User

Are you still worried about Setcontentview ()?

Or, are you still worried about Findviewbyid ()?

If yes, please look down ...

IOC As we all know, control inversion (inversion of controls) is the project layering specific theory overview individual to check the information ...

Before listening to ' senior ' to mention the annotations, but then did not know is God horse, only know that sounds very magical, because ' Predecessor said ': Do not have to write what Setcontentview and Findviewbyid such code, especially Findviewbyid, presumably everyone is deep experience, if an interface has 10 view, there will be 10 lines of Findviewbyid ... Now, with the annotations, you don't need a line!

First, we need two annotation files:

Contentview.java:

Package Com.yx.viewinject_demo.ui;import Java.lang.annotation.elementtype;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.target;/*** *  * This class describes: Contentview is used on a class and is used primarily to indicate the layout file that the activity needs to use *  * @author: CS YX * @version: 1.0 * @date: 2014-10-14 a.m. 9:55:03 */@Target (elementtype.type)/* * @Target indicates where the annotation can be used, possible types of type (Class), FIELD (member variable) * Possible types: type, FIELD, METHOD, PARAMETER, CONSTRUCTOR *, local_variable, Annotation_type, Package */@Retention (retentionpolicy.runtime)/* @ Retention: Indicates the level at which the annotation information needs to be saved; we set it here as runtime. Possible types: SOURCE, CLASS, RUNTIME */public @interface contentview {int value ();}

This annotation file is used to inject the layout, placed in front of the activity class, indicating the layout of the activity reference:

Then the second annotation file:

Viewinject.java

Package Com.yx.viewinject_demo.ui;import Java.lang.annotation.elementtype;import java.lang.annotation.Retention; Import Java.lang.annotation.retentionpolicy;import java.lang.annotation.target;/*** *  * This class describes: used on member variables, The ID used to specify the view *  * @author: CS YX * @version: 1.0 * @date: 2014-10-14 morning 9:57:21 */@Target (Elementtype.field)/* * @Target indicates that the Annotations can be used anywhere, possible types type (Class), FIELD (member variable) * Possible types: type, FIELD, METHOD, PARAMETER, CONSTRUCTOR *, local_variable, Annotation_ TYPE, Package */@Retention (retentionpolicy.runtime)/* @Retention indicate: Indicates the level at which the annotation information needs to be saved; we set it here as runtime. Possible types: SOURCE, CLASS, RUNTIME */public @interface viewinject {int value ();}

This annotation file is used to inject the view control to specify the ID of the view

@ViewInject (R.ID.BT1) private button bt1; @ViewInject (R.ID.BT2) private button bt2;

Now, we need to define a Viewinject helper class, which is the core class.

Viewinjectutils.java:

Package Com.yx.viewinject_demo.utils;import Java.lang.reflect.field;import Java.lang.reflect.method;import Com.yx.viewinject_demo.ui.contentview;import Com.yx.viewinject_demo.ui.viewinject;import android.app.Activity;/* * * * This description is: Annotation Help class * * @author: CS YX * @version: 1.0 * @date: 2014-10-14 a.m. 10:09:35 */public class Viewinjectutils {pri Vate static final String method_set_contentview = "Setcontentview";p rivate static final String method_find_view_by_id = "F Indviewbyid ";/** * * This method describes: inject layout and view control * * @param activity * Activity object */public static void inject (Activi Ty activity) {//Inject layout file Injectcontentview (activity);//inject All View control injectviews (activity);} /*** * * This method describes: inject layout file * * @param activity * Activity object */private static void Injectcontentview (activity AC tivity) {class<? extends activity> clazz = Activity.getclass ();//query class for annotations Contentview Contentview = Clazz.getannotation (Contentview.class); if (Contentview! = null) {//exists, not empty//annotated Idint ContentviewlaYoutid = Contentview.value (); try {//Get Method Object method = Clazz.getmethod (Method_set_contentview,int.class);// Set properties accessible (private) method.setaccessible (true); Method.invoke (activity, Contentviewlayoutid);} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}  /** * * This method describes: inject all controls * * @param activity * Activity object */private static void Injectviews (activity activity) {//Get class object class<? extends activity> clazz = Activity.getclass ();//Get all properties of class object (public, private) field[] fields = Clazz.getdeclare Dfields (); for (Field field:fields) {Viewinject viewinject = field.getannotation (Viewinject.class); if (viewinject! = nul L) {//Get view of Idint Viewinjectid = Viewinject.value (); if (Viewinjectid! =-1) {method Method;try {//Get Method Object method = Cl Azz.getmethod (Method_find_view_by_id,int.class); Object Resview = Method.invoke (activity, viewinjectid);//setting properties are accessible ( Private) field.setaccessible (true); Field.set (activity, Resview);} catch (Exception e) {//TODO auto-generated catch Blocke.prinTstacktrace ();}}}}} 

The code is basically finished, leaving only the last mainactivity:

Mainactivity.java:

Package Com.yx.viewinject_demo.ui;import Android.app.activity;import Android.os.bundle;import android.view.Menu; Import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Com.yx.viewinject_demo. R;import Com.yx.viewinject_demo.utils.toastutils;import com.yx.viewinject_demo.utils.viewinjectutils;/** * * This class describes the : Application Main Interface * * @author: CS YX * @version: 1.0 * @date: 2014-10-21 morning 10:08:31 */@ContentView (value = r.layout.activity_main) pub Lic class Mainactivity extends Activity implements Onclicklistener {@ViewInject (r.id.bt1) private Button bt1; @ViewInject (R.ID.BT2) Private Button bt2; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate ( savedinstancestate);//inject layout and control viewinjectutils.inject (mainactivity.this);//Set onclick event Bt1.setonclicklistener ( Mainactivity.this); Bt2.setonclicklistener (mainactivity.this);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getMenuinflater (). Inflate (R.menu.main, menu); return true;} @Overridepublic void OnClick (View v) {//TODO auto-generated method Stubswitch (V.getid ()) {case R.id.bt1:toastutils.show Short (This, "Hi,this is bt1 ..."); Break;case R.id.bt2:toastutils.showshort (This, "Hello,this is BT2 ..."); Break;default : Break;}}}

The code is finished, as follows:


Demo Address Click to download demo

Thank you: http://blog.csdn.net/lmj623565791/article/details/39269193







Android Annotated IOC framework "Viewinject" (i)

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.