A Android uses custom annotations to initialize controls

Source: Internet
Author: User

In general, we develop Android applications, initialize the control using Findviewbyid (), but after a project is developed, you will find that many of these codes are actually duplicated. At this point you'll find out how easy it is to have Java's own annotations (Annotation).

One, the design of a contentview annotation

Because this annotation is to be shipped on the activity class, we must declare @elementtype.type.

     @Target (Elementtype.type)     @Retention (retentionpolicy.runtime) public     @interface Contentview {int value ();    }

Second, the design of a contentwidget annotation

Because this annotation is going to be shipped on the class member variable, we have to declare @elementtype.fileld. Class member variable specifies the control object that we declare

@Target (Elementtype.field) @Retention (retentionpolicy.runtime) public @interface contentwidget {int value ();}

Thirdly, design a tool class to extract and process the information of the above custom annotation class.

     public static void Injectobject (Object object, activity activity) {class<?> ClassType = Object.getclass ();//The Class Whether there is a contentview type of annotation if (classtype.isannotationpresent (Contentview.class)) {//returns annotations of the existing Contentview type Contentview annotation = classtype.getannotation (contentview.class); try {//Returns a method object that reflects the specified public member methods of the class or interface represented by this class object. method = Classtype.getmethod ("Setcontentview", Int.class); method.setaccessible (true); int resId = Annotation.value (); Method.invoke (object, resId);} catch (Nosuchmethodexception e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch ( IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch (Illegalaccessexception e) {/ /TODO auto-generated catch Blocke.printstacktrace ();} catch (InvocationTargetException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} Returns an array of Field objects that reflect the member variables of the class or interface declaration represented by this class object, including public, protected, default (package) access, and private member variables, but not inherited member variables. field[] fields = Classtype.getdeclaredfields ();(Null! = Fields && fields.length > 0) {for Field field:fields} {//This member variable exists contentwidget type of annotation if (field.isannotationpresent (Contentwidget.class)) { contentwidget annotation = field.getannotation (contentwidget.class); int viewId = Annotation.value (); View view = Activity.findviewbyid (ViewId), if (null! = view) {try {field.setaccessible (true); Field.set (object, view);} catch (IllegalArgumentException e) {//TODO auto-generated catch Blocke.printstacktrace ();} catch ( Illegalaccessexception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}} }


Iv. How do we use the annotations we define in the activity class?

@ContentView (r.layout.activity_main) public class Mainactivity extends activity {@ContentWidget (r.id.tv) Private TextView TextView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); /Setcontentview (R.layout.activity_main); Viewutils.injectobject (This), Textview.settext ("Custom Comment");

Although the pre-preparation of a bit more work, but the post-development process for the loading of layout and initialization of the control will be a lot of trouble, this for large projects, especially important, sharpening does not accidentally cut timber workers!


A Android uses custom annotations to initialize controls

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.