First, Introduction
It would be tedious to find the desired view in the layout file via Findviewbyid in Android, and add a lot of view in the activity to initialize it. Of course Google you will find there are many Android annotations frameworks. such as the more famous "Android Annotations", such a framework is complex, it is more troublesome to use, there are some bugs, the first use also took a lot of time to study. Maybe you only want to use the Inject view feature in the project, or you want to know how the implementation works. This article is mainly to solve these two problems, to achieve one of the simplest viewinject.
Second, the principle
The principle is to dynamically set the value of this field after the activity has been loaded by finding the fields of the activity using annotations and then by the way of Java reflection.
1, first you need to understand how the Java annotations work, if you do not understand can first look at the relevant information, this relatively simple answer. First define our annotation class:
1 /**2 * View Inect by ID3 * 4 * @authorLucky5 * 6 */7@Target (Elementtype.field)//indicates the use of the field8@Retention (Retentionpolicy.runtime)//indicates that the life cycle is run-time9 Public@InterfaceViewinject {Ten intValue ()default0; One}
View Code2, we need to define a baseactivity, in this class to parse the annotations
1 /**2 * 3 * @authorLucky4 * 5 */6 Public Abstract classBaseactivityextendsfragmentactivity {7 /**8 * Get content view layout ID9 * Ten * @return One */ A Public Abstract intGetlayoutid (); - - the @Override - protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (Getlayoutid ()); + Autoinjectallfield (); - } + /** A * Parsing Annotations at */ - Public voidAutoinjectallfield () { - Try { -Class<?> Clazz = This. GetClass (); -field[] fields = Clazz.getdeclaredfields ();//get the fields declared in activity - for(Field field:fields) { in //See if this field has our own custom annotation class flags. - if(Field.isannotationpresent (Viewinject.class)) { toViewinject inject = field.getannotation (viewinject.class); + intID =Inject.value (); - if(ID > 0) { theField.setaccessible (true); *Field.set ( This, This. Findviewbyid (ID));//set the value for the field we're looking for. $ }Panax Notoginseng } - } the}Catch(illegalaccessexception e) { + e.printstacktrace (); A}Catch(IllegalArgumentException e) { the e.printstacktrace (); + } - } $}
View Code3, the completion of the above steps is how to use, the sample code is as follows:
1 Public classTestactivityextendsbaseactivity {2 3 @ViewInject (r.id.claim_statement)4 PrivateWebView Mwebview;5 6 7 @Override8 Public intGetlayoutid () {9 //TODO auto-generated Method StubTen returnR.layout.activity_claim; One } A -}
View Code
That's all you can do. A few lines of code can greatly improve productivity.
PS: It is recommended to use a third-party full annotation framework.
Android Simple annotation View (Java Reflection Implementation)