Annotations:
In fact, this thing says the mystery is not mysterious, because we have been useful in various projects. As seen in Spring3, it can be seen in some of the rapid development frameworks of Android. But said it is not mysterious, in fact, is quite mysterious, although I have been exposed to this function, but never careful to pay attention to how it is achieved. This time I'm going to use the Android view annotations without understanding its principles, so that he is no longer so mysterious.
Baidu know to untie my confusion on the note, the following is Baidu know on:
Annotation objects can be divided into packages, classes, fields, construction methods, methods, parameters and other annotations.
With the above understanding, we will not say much, directly on the code
<pre name= "code" class= "java" style= "Font-weight:bold;" >public<pre name= "code" class= "java" style= "Font-weight:bold;" >public abstract <span style= "font-family:arial, Helvetica, Sans-serif;" >class Basebindactivity extends Activity {</span>
@Overrideprotected final void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Bindlayout (); try {BindView ();} catch (Exception e) {e.printstacktrace (); throw new RuntimeException ("Bind error");} Oncreated (savedinstancestate);} public void oncreated (Bundle savedinstancestate) {} @Retention (Retentionpolicy.runtime)//Run-time Get @target ( Elementtype.field)//annotation for the field protected @interface bindview {int viewId ();} @Retention (retentionpolicy.runtime)//Run-time get @target (elementtype.type)//Annotation for class protected @interface Bindlayout{int LayoutID ();} private void BindView () throws IllegalArgumentException, Illegalaccessexception {
<span style= "White-space:pre" ></span>//get all member variables field[] fs = GetClass (). Getdeclaredfields (); if (fs! = NULL {for Field f:fs) {BindView ma = f.getannotation (bindview.class); System.err.println (F.getname ()); if (ma! = null) {<span style= "font-family:arial, Helvetica, Sans-serif;" >//Start binding If you can find annotations </span>
View v = Findviewbyid (Ma.viewid ()), if (v! = null) {f.setaccessible (true); F.set (this, F.gettype (). Cast (v));} else {throw n EW RuntimeException ("The view with the specified ID not found binding to:" + f.getname ());}}}} private void Bindlayout () {bindlayout bl = getclass (). Getannotation (Bindlayout.class); if (bl==null) {throw new RuntimeException ("No layout file is bound");} int id = Bl.layoutid (), if (id==0) throw new RuntimeException ("Invalid layout file bound"); Setcontentview (ID);}}
1. Inherit activity, easy to use some of its methods, such as Setcontentview,findviewbyid, etc.
2. Declaration of Annotations Interface: Bindview,bindlayout
3. Re-OnCreate the method, add the final keyword to the method, to ensure that no quilt class overwrite
4. Implement the Bindlayout method to complete the binding layout file
5. Implement the BindView method to complete the binding of the member variables of view and its subclasses
6. Declare a oncreated method that allows subclasses to replicate and complete the logic that should be done in the OnCreate method
Now the parent class prepares the number to see how the subclass uses
@BindLayout (LayoutID = r.layout.activity_main) public class Mainactivity extends Basebindactivity {@BindView (viewId = r.id.tv) private TextView TV; @Overridepublic void oncreated (Bundle savedinstancestate) {componentname cn = new ComponentName (This, "Testauto. B "); Packagemanager pm = Getpackagemanager (); int state = Pm.getcomponentenabledsetting (CN); Tv.settext (state+ "" + Android.os.Build.MODEL);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {return true;}}
1. Inheriting basebindactivity
2. Add @BindLayout (LayoutID = R.layout.activity_main) where the class declaration is, and R.layout.activity_main is the layout file you want to bind
3. When declaring a member view variable, add @BindView (viewId = r.id.tv) r.id.tv is the ID of the view you want to bind to
Implementation of annotation loading for Android view