Presumably the android has been developed using a number of frameworks, but the framework will use some, annotations to help us, save a lot of code development volume. It also makes the code more concise.
But annotations are implemented through Java reflection, which of course may sacrifice some performance.
The following is a simpler bound control that I wrote and triggers its Onclicklistem event.
Just look at the annotation method first. Relatively simple, clear
public class Mainactivity extends Fragmentactivity {<span style= "White-space:pre" ></span>//the button control corresponding to the binding, The event name is <span style= "font-family:arial, Helvetica, Sans-serif;" >onClickCallback</span> @InjectView (id = r.id.button1, Clickmethod = "Onclickcallback") Private Button Button1 ; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Uibindutil.bind (this);//Initialize Binding} @ClickMethod (R.id.button1) public void Onclickcallback (view view) {// Event Toast.maketext triggered (mainactivity.this, "I am Annotation", 1). Show ();}}
Next is the binding initialization class
Package Com.ferris.ioc;import Java.lang.reflect.field;import Java.lang.reflect.method;import android.app.Activity; public class Uibindutil {/** * Binding initializes the properties of each space and events * @param activity */public static void bind (activity activity) {class<? > CL = Activity.getclass (); Bindviews (activity, CL); Bindmethods (activity, CL);} /** * The corresponding event method for the button binding * @param activity * @param cl */private static void Bindmethods (activity activity, class<?> CL) {for (Method method:cl.getDeclaredMethods ()) {Clickmethod OC = method.getannotation (Clickmethod.class); if (oc! = null) { int ids[] = Oc.value (); for (int id:ids) {Oneventlistener listener = new Oneventlistener (activity). Setmclick (id,method.ge Tname ()); Activity.findviewbyid (ID). Setonclicklistener (Listener);}}} /** * Initialize button * @param activity * @param cl */private static void Bindviews (activity activity, class<?> Cl) {try {for (Field field:cl.getDeclaredFields ()) {Injectview av = field.getannotation (injectview.class); if (av! = null) {field.setaccessIble (True); Setview (field, activity, av.id ());}}} catch (Exception e) {e.printstacktrace ();}} private static void Setview (Field view, activity activity, int id) throws IllegalArgumentException, illegalaccessexception {view.set (activity, Activity.findviewbyid (ID));}}
Injection class for click Method
Package Com.ferris.ioc;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Retention (retentionpolicy.runtime) @ Target (elementtype.method) public @interface Clickmethod {int[] value () default {};}
The injected class that binds the control ID
Package Com.ferris.ioc;import Java.lang.annotation.elementtype;import Java.lang.annotation.retention;import Java.lang.annotation.retentionpolicy;import java.lang.annotation.Target; @Target (Elementtype.field) @Retention ( retentionpolicy.runtime) Public @interface injectview {public int id (); Public String Clickmethod () default ""; Click the callback method (the method parameter must have only one view! )}
The binding method event triggers the management class, and the management class is simple and I have simply commented on it. I believe it looks easy.
Package Com.ferris.ioc;import Java.lang.reflect.method;import Java.util.hashmap;import java.util.Map;import Android.annotation.suppresslint;import Android.app.activity;import Android.text.textutils;import Android.view.view;import Android.view.View.OnClickListener; @SuppressLint ("Usesparsearrays") public class Oneventlistener implements Onclicklistener{private Activity activity;private static Map<integer, string> Listeners = new Hashmap<integer, string> (); We keep the control ID and the association of the Method name public Oneventlistener (activity activity) {//TODO auto-generated constructor stubthis.activity= Activity;} Public Oneventlistener () {}public Oneventlistener setmclick (Integer id,string name) {//Adds an association and returns a listener to the view to set the Listener//TODO A Uto-generated method Stublisteners.put (ID, name); return this;} @Overridepublic void OnClick (View v) {///when there is an event trigger, pass the ID to get the corresponding method, pass the parameter to the method to trigger the corresponding method. Can//TODO auto-generated method stubif (listeners==null| | Listeners.size () <=0) {return;} String Methodname=listeners.get (V.getid ()); if (textutils. IsEmpty (MethodName)) {return;} try {//through the class to find the corresponding method and the parameter type method Callbackmethod = Activity.getclass (). Getdeclaredmethod (MethodName, View.clas s); Callbackmethod.setaccessible (TRUE); Pass the corresponding parameters to the past Callbackmethod.invoke (activity, v); } catch (Exception e) {e.printstacktrace (); }}}
Notes on the Android IOC binding control and event case code simple comment explanation