Usage of annotations in Android

Source: Internet
Author: User

Use of annotations in Android programs

What is an annotation:

In Java, annotations are also called "metadata," which provides a formalized way for us to add information to the source code, allowing us to use the data at a later time. Rather, the annotations bind the data to the meta code to a certain extent, not to keep the data in an external file. Of course, this is only about Android development-related technologies, as for annotations I will be in the Java Learning Summary of the detailed research and summary. So, let's take an example to introduce the ingenious application of annotations in Android development.

Example Description:

Here, we use annotations to replace the Findviewbyid (...) of the initialized resource object in Android. method, as shown in the following code:

Structure of my project:

Notes section:

Contentview.java:

/**

* Inject Contentview

*/

@Target (ElementType. TYPE)

@Retention (Retentionpolicy. RUNTIME)

Public @interface Contentview {

int value () default 0;

}

Injectview.java:

/**

* Inject View of Contentview

*/

@Target (ElementType. FIELD)

@Retention (Retentionpolicy.runtime)

Public @interface Viewinject {

int value ();

int parentresid () default 0;

}

Resource Initialization section:

Viewinjectobj.java:

int Parentresid;

Object value;

@Override

Public boolean equals (Object obj) {

Boolean result;

if ( this = = obj) {

result = true;

}

if (! (obj instanceof viewinjectobj)) {

result = false;

}

Viewinjectobj that = (viewinjectobj) obj;

if (parentresid ! = that. Parentresid) {

result = false;

}

if (null = = value) {

return result = (null = = that. Value);

}

result = value. Equals (that. Value);

return result;

}

@Override

Public int hashcode () {

int result = value. hashcode ();

result = * result + Parentresid;

return result;

}

@Override

Public String toString () {

return "Viewinjectobj [parentresid=" + parentresid + ", value=" + value

+ "]";

}

Viewselector.java:

/**

* Find View ID Resources

*/

Public class Viewselector {

Private Activity curractivity;

Private View Currview;

Public Viewselector (activity activity) {

curractivity = activity;

}

Public Viewselector (view view) {

Currview = view;

}

Public View Findviewbyid (int id) {

return null = = Currview ? curractivity. Findviewbyid (ID): currview. Findviewbyid (ID);

}

Public View findviewbyinfo (viewinjectobj info) {

return Findviewbyid ((Integer) info. Value, info. parentresid);

}

Public View Findviewbyid (int ID,int pId) {

View PView = null;

if (0 < PId) {

PView = Findviewbyid (pId);

}

View Currview = null;

if (null ! = PView) {

Currview = Pview.findviewbyid (ID);

} Else {

Currview = Findviewbyid (ID);

}

return Currview;

}

}

Mapping Method Section:

Injectconfig.java:

/**

* Inject View methods

*/

Public class Injectconfig {

Static Final String TAG = "Injectconfig";

Public Injectconfig () {

}

//Inject Contentview  

Public void Injectcontentview (Object obj,class<?> objclass) {

contentview contentview = objclass.getannotation (contentview. Class);

if (null ! = Contentview) {

Try {

method = Objclass.getmethod ("Setcontentview", int. Class);

Method.invoke (obj, Contentview.value ());

} Catch(Throwable tr) {

LOG.E (TAG, Tr.getmessage (), TR);

}

}

}

Inject view

Public void Injectview (Object obj,viewselector viewselector,class<?> objclass) {

field[] fields = Objclass.getdeclaredfields ();

if (null ! = Fields && 0 < fields. length) {

for (Field field:fields) {

viewinject viewinject = field.getannotation (viewinject. Class);

if (null ! = viewinject) {

Try {

View view = Viewselector.findviewbyid (Viewinject.value (), Viewinject.parentresid ());

if (null ! = view) {

Field.setaccessible (true);

Field.set (obj, view);

}

} Catch(Throwable tr) {

LOG.E (TAG, Tr.getmessage (), TR);

}

}

}

}

}

}

To invoke the Tools section:

Viewutilsvc.java:

Public class viewutilsvc {

Static Injectconfig config = null;

Public Static void Inject (activity activity) {

Inject (activity,new viewselector (activity));

}

Public Static void Inject (view view) {

Inject (view,new viewselector (view));

}

Public Static void inject (Object obj, view view) {

Inject (obj, new viewselector (view));

}

Static void Inject (Object obj,viewselector viewselector) {

if (null = = config) {

config = new injectconfig ();

}

Class<?> objclass = Obj.getclass ();

config. Injectcontentview (obj, objclass);

config. Injectview (obj, Viewselector, objclass);

}

}

Front Display section:

Mainactivity.java:

@ContentView (R.layout. Activity_main)

Public class Mainactivity extends fragmentactivity {

@Override

protected void onCreate (Bundle savedinstancestate) {

Super. OnCreate (savedinstancestate);

Setcontentview (R.layout.activity_main);

Viewutilsvc.inject (this);

if (savedinstancestate = = null) {

Getsupportfragmentmanager ()

. BeginTransaction ()

. Add (r.id. Container, new placeholderfragment ())

. commit ();

}

}

Public Static class Placeholderfragment extends Fragment {

@ViewInject (R.id. Tvhelloworld)

TextView Tvhelloworld;

Public Placeholderfragment () {

}

@Override

Public View Oncreateview (layoutinflater inflater, ViewGroup container,

Bundle savedinstancestate) {

View Rootview = Inflater.inflate (r.layout. Fragment_main, container,false);

Viewutilsvc.inject (this, rootview);

String Hello = getresources (). getString (r.string. Text_helloworld_tv);

Tvhelloworld. SetText (hello);

return Rootview;

}

}

}

As shown in the code above, we used annotations to dynamically add data to the code, and then use the reflection mechanism to get the annotated data;

Replaced native Setcontentview (...) with @contentview (...) section, using the Viewutilsvc.inject (...) The corresponding injection can be done. In addition, this is done to simplify the initialization of the object view. The specific operation is as follows:

Well, here's the Hello World on our TextView, and if you have any questions, please discuss them in the comments.

/**

* Technical Exchange QQ Group :179914858

*/

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.