The code for the activity to be posted first:
@ContentView(R.layout.activity_main) Public class mainactivity extends appcompatactivity { @ViewInject(R.id.text_view)PrivateTextView TextView;@OnClick(R.id.text_view)Private void OnClick(View view) {Textview.settext ("I'm textview after click."); }@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Viewinjectutils.inject ( This); Textview.settext ("I'm the TextView in front of the click."); }}
Contentview (Class) annotation implementations:
1. Define Annotations
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) public @interface ContentView { intvalue(); }
2. Implementation annotations
Private Staticvoid Injectcontentview (activity activity) { Class<? extends activity> clazz = activity. GetClass (); Contentview Contentview = clazz. getannotation (Contentview. class); if (contentview ! = null) { //If this annotation is present on the activity, take out the value of the annotation corresponding to the previous layout file. int layoutid = Contentview.value ();Try{Method Setviewmethod = Clazz.getmethod ("Setcontentview", Int.class); Setviewmethod.invoke (activity, LayoutID); }Catch(Exceptione) {e.printstacktrace (); } } }
field annotation Implementation
1. Define Annotations
@Target(ElementType.FIELD)@Retention(RetentionPolicy.RUNTIME)public @interface ViewInject { intvalue();}
2. Implementation annotations
Private Staticvoid Injectview (activity activity) { Class<? extends activity> clazz = activity. GetClass (); Get all member variables for activity Field[] fields = clazz. Getdeclaredfields (); for (field field : fields) { //Get the viewinject annotation above each member variable and return NULL if not .Viewinject viewinject = field.getannotation (Viewinject.class);if(Viewinject! =NULL) {int viewId = Viewinject.value (); View view = Activity.findviewbyid (viewId);Try{field.setaccessible (true); Field.set (activity, view); }Catch(Illegalaccessexception e) {E.printstacktrace (); } } } }
Interface (method) annotation implementation
1. Define Annotations
@Target(ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)public @interface OnClick { intvalue();}
2. Implementation annotations
Public class viewinjectutils { Public Staticvoid inject (activity activity) {Injectcontentview (activity); Injectview (activity); Injectevent (activity); }Private Staticvoid Injectevent (FinalActivity activity) { Class<? extends activity> clazz = activity. GetClass (); Method [] methods = Clazz. Getdeclaredmethods (); for (final Method method2 : methods) {OnClick click = Method2.getannotation (Onclick.class);if(Click! =NULL) {int[] viewId = Click.value (); Method2.setaccessible (true); Object listener = proxy.newproxyinstance (View.OnClickListener.class.getClassLoader (),New Class[]{View.OnClickListener.class},NewInvocationhandler () {@Override PublicObject Invoke (Object proxy, Method method, object[] args) throws Throwable {returnMethod2.invoke (activity, args); } });Try{ for(int id:viewid) {View v = Activity.findviewbyid (id); Method Setclicklistener = V.getclass (). GetMethod ("Setonclicklistener", View.OnClickListener.class); Setclicklistener.invoke (v, listener); } }Catch(Exceptione) {e.printstacktrace (); } } } }
Java Basics Annotations implementation in Android