Android annotation Learning (1)

Source: Internet
Author: User

For the concept of annotations at the beginning is not very understanding, read the other People's blog, refer to the implementation of the example to begin to understand and use. When I first started writing Android projects, it was common to find a definition control and initialize the control to call Findviewbyid, but when a layout page (similar to submitting a form page) controls extra time, the code becomes bloated and inconvenient for subsequent maintenance. The others recommend it. Using the Xutils framework to provide annotations to bind the control, I feel that the annotation is intuitive to see that he combined the declaration with the bound control, the code is much clearer. Do you have?

1     2@ViewInject (id =r.id.lv_test1)3     PrivateListView Lv_test1;4     5  6 7 8 @Override9     protected voidonCreate (Bundle savedinstancestate) {Ten         Super. OnCreate (savedinstancestate); One Setcontentview (r.layout.activity_main); A  -}

The following focus needs to understand the concepts and syntax used by annotations

Android API description can be seen it is belonging to the Java.lang package and three pieces of content to describe the meta-annotations , interfaces, enumerations, respectively;

The following main introduction meta-annotations:

1.annotations-- yuan Annotations

1) documented--defines a meta-annotation that is used to represent a comment and is considered part of the public interface. Documented is a markup annotation with no members.

2) inherited--defines a meta-annotation that represents the automatic inheritance of annotations.

3)Retention-Defines the meta-annotations used to determine the retention range of the annotations. (that is, the description of the annotation in which the scope is valid)

4)Target--defines what ElementType annotations can be used for meta annotations. (that is, where the described annotations can be used)

You can refer to (in-depth understanding of Java: Annotations (Annotation) to get started with custom annotations)

2. Usage See below:

1 ImportJava.lang.annotation.ElementType;2 Importjava.lang.annotation.Retention;3 ImportJava.lang.annotation.RetentionPolicy;4 ImportJava.lang.annotation.Target;5 6 @Target (Elementtype.field)7 @Retention (retentionpolicy.runtime)8  Public@InterfaceViewinject {9      Public intID ();Ten}
@Target (Elementtype.field)-refers to the annotation for a field

@Retention (Retentionpolicy.runtime)--refers to the function at run time

To define the annotation format:
Public @interface annotation name {definition body}

How do you bind a custom annotation to a control?
1 /**2 * Initialization of Findviewbyid annotations3      */4      Public voidInitview (Object windowobj) {5         //gets the field declared by the class that contains the Windowobj object6field[] Fields =Windowobj.getclass (). Getdeclaredfields ();7         if(Fields! =NULL&& fields.length > 0) {8              for(Field field:fields) {9                 Try {Ten                     //instead of changing the access rights of a method to public, the permission control check for Java is canceled OneField.setaccessible (true); A                     if(Field.get ( This) !=NULL) -                         Continue; -                     //gets the annotation object with the Viewinject field theViewinject Viewinject =Field -. getannotation (Viewinject.class);// -                     if(Viewinject! =NULL) { -                         intViewId =viewinject.id (); +                         //bind the field to a control -Field.set ( This, Findviewbyid (viewId)); +                     } A}Catch(Exception e) { at e.printstacktrace (); -                 } -             } -         } -}

Android annotation Learning (1)

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.