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