Android IOC annotation binding control and event case code simple annotation explanation, androidioc
I think android has used some frameworks. However, some annotations are used in the frameworks to help us and save a lot of code development workload. It also makes the code more concise.
However, annotations are implemented through java reflection and may sacrifice some performance.
The following is a simple binding control that I wrote and triggers its OnclickListem event.
// Directly look at the annotation method. Relatively simple and clear
Public class MainActivity extends FragmentActivity {<span style = "white-space: pre"> </span> // bind the corresponding button control, 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) {// triggered event Toast. makeText (MainActivity. this, "I am an annotation", 1 ). show ();}}
// Next is the bound initialization class
Package com. ferris. ioc; import java. lang. reflect. field; import java. lang. reflect. method; import android. app. activity; public class UIBindUtil {/*** bind and initialize attributes of each space and events * @ param activity */public static void bind (Activity activity) {Class <?> Cl = activity. getClass (); bindViews (activity, cl); bindMethods (activity, cl );} /*** bind the corresponding event Method to the button * @ 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. getName (); activity. findViewById (id ). setOnClickListener (listener) ;}}}/*** initialization 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 of the 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 {};}
// Injection class bound to 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 !)}
// Bind method event trigger management class. The management class is relatively simple. I have commented on it briefly. 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 <Inte Ger, String> listeners = new HashMap <Integer, String> (); // we save the association public OnEventListener (Activity activity) of the Control ID and method name) {// TODO Auto-generated constructor stubthis. activity = activity;} public OnEventListener () {} public OnEventListener setmClick (Integer id, String name) {// Add Association, and return to the listener interface to set the listener for View // TODO Auto-generated method stublisteners. put (id, name); return this ;}@ Overridepublic void onClick (View v) {// When an event is triggered, the corresponding method is obtained using the id and the parameters are passed to the method to trigger the corresponding method. You can // TODO Auto-generated method stubif (listeners = null | listeners. size () <= 0) {return;} String methodName = listeners. get (v. getId (); if (TextUtils. isEmpty (methodName) {return;} try {// use the class to find the corresponding Method and parameter type Method callbackMethod = activity. getClass (). getDeclaredMethod (methodName, View. class); callbackMethod. setAccessible (true); // pass the corresponding parameter callbackMethod. invoke (activity, v);} catch (Exception e) {e. printStackTrace ();}}}