Java Basics Annotations implementation in Android

Source: Internet
Author: User

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

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.