How the Android annotations (annotation) Implement binding events

Source: Internet
Author: User

Annotations are a very elegant way of writing, and our code is simple and quick and easy to write code. The following is an example of binding onclick practice to illustrate the principle of annotations.

1. Define the onclick annotation class
@Target (Elementtype.method) @Retention (retentionpolicy.runtime) @BaseEvent (listenersetter = "Setonclicklistener",            Listenertype = View.OnClickListener.class,            methodName = "onclick") public @interface onclick {     int[] Values ();}

BaseEvent is used to develop the method name to bind, the listener of the event, and the method name of the binding execution, BaseEvent code is as follows

@Target (Elementtype.type) @Retention (retentionpolicy.runtime) public @interface baseevent {class<?> Listenertype (); String Listenersetter (); String methodName ();}
2. How to use custom annotations

@OnClick (R.ID.DOWNLOAD_BTN) public void testupload (view view) {}
3, the interpretation of annotations (emphasis):

Idea: You can use the method of adding onclick annotations (testupload) in our annotation object to reflect the method object and bind the object to the event. Onclicklister in the dynamic proxy view event, adding a Method object (Testupload) to the proxy object. The addition of the method is done in the Eventlistenermanager management class.

Define the dynamic proxy class in Eventlisernermanager:

public static void Addeventmethod (Method method,annotation annotation,object value,object handler,viewfinder Finder) { Try{view View = finder.findviewbyinfo (value), if (view!=null) {baseevent baseevent = Annotation.annotationtype (). Getannotation (Baseevent.class); class<?> Listenertype = Baseevent.listenertype (); String listenersetter = Baseevent.listenersetter ();  String methodName = Baseevent.methodname (); Object listener = listenercache.get (value, Listenertype); Boolean addmethod = False;dynamichandler Dymichandler = null;if (listener!=null) {Dymichandler = (Dynamichandler) Proxy.getinvocationhandler (listener); Addmethod = (Handler.equals (Dymichandler.gethandler ())); if (Addmethod) { Dymichandler.addmethod (MethodName, method);}} if (!addmethod) {Dymichandler = new Dynamichandler (handler);d Ymichandler.addmethod (MethodName, method); listener = Proxy.newproxyinstance (Listenertype.getclassloader (), new Class<?>[]{listenertype},dymichandler); Listenercache.put (value, listenertype, listener);} SEtlistenermethod Settermethod = View.getclass (). GetMethod (Listenersetter, Listenertype); SetterMethod.invoke (View, listener);}} catch (Exception e) {logutils.e (E.getmessage (), E);}}
Listener = Proxy.newproxyinstance (Listenertype.getclassloader (), new Class<?>[]{listenertype},dymichandler); Dynamic agent of the Onclicklistener. The cache for proxy objects in a class:

Cache proxy object public final static doublekeyvaluemap<object,class<?>, object> listenercache = null;


To parse the annotation Add method:

Method[] methods = Handlertype.getdeclaredmethods (), if (methods.length>0) {for (Method method:methods) {annotation[ ] Annotations = Method.getdeclaredannotations (); for (Annotation annotation:annotations) {class<?> Antype = Annotation.annotationtype ();//Get annotation's Classif (antype.getannotation (baseevent.class)!=null) { Method.setaccessible (true); Try{method Valuemethod = Antype.getdeclaredmethod ("values"); Object values = Valuemethod.invoke (annotation), int len = Array.getlength (values), if (len>0) {for (int i=0;i<len;i++) {   Object value = Array.get (values, i);   Eventlistenermanager.addeventmethod (method, annotation, value, Handlertype, finder);}}}    catch (Exception e) {e.printstacktrace ();}}}}
Here you can get the custom method and the annotations themselves. You can study the use of Java annotation in a less familiar way.

By the above method can realize the binding of the annotation event, you can define the annotations of the event, gracefully start your code bar!











How the Android annotations (annotation) Implement binding events

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.