On the basis of the previous study, we write a viewutils annotation framework class, the unclear principle of the previous source code
Packageim.weiyuan.com.viewutils;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.os.Bundle;ImportAndroid.view.View;ImportAndroid.widget.Button;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast; Public classMainactivityextendsappcompatactivity {@ViewInject (r.id.tv_main_title)PrivateTextView Tv_main_title; PrivateButton Btn_main; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); //Be sure to initialize the annotations hereViewutils.inject ( This); } //the login parameter must be a view view@OnClick (R.id.btn_main) Public voidLogin (view view) {Toast.maketext (mainactivity. This, "The event was clicked", Toast.length_long). Show (); }}
Packageim.weiyuan.com.viewutils;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;/*** Created by Wei.yuan on 2017/7/21.*///specify the declaration period for annotations@Retention (retentionpolicy.runtime) @Target (Elementtype.method) Public@InterfaceOnClick {intvalue ();}
Packageim.weiyuan.com.viewutils;ImportJava.lang.annotation.ElementType;Importjava.lang.annotation.Retention;ImportJava.lang.annotation.RetentionPolicy;ImportJava.lang.annotation.Target;/*** Created by Wei.yuan on 2017/7/21.*///specify the declaration period for annotations@Retention (retentionpolicy.runtime) @Target (Elementtype.field) Public@Interfaceviewinject{intValue ();//If this is defined as ID int id (), the user must write @ViewInject (id = r.id.tv_main_title) when using, if defined as int value (); //user access can be written as @viewinject (R.id.tv_main_title)}
Packageim.weiyuan.com.viewutils;Importandroid.app.Activity;ImportAndroid.view.View;ImportJava.lang.reflect.Field;Importjava.lang.reflect.InvocationTargetException;ImportJava.lang.reflect.Method;/*** Created by Wei.yuan on 2017/7/21.*/ Public classViewutils { Public Static voidinject (activity activity) {BindView (activity); BINDBTN (activity); } /*** operation of the button * **/ Private Static voidBINDBTN (Finalactivity activity) {Class Clazz=Activity.getclass (); /*** Get all the methods **/method[] Methods=Clazz.getdeclaredmethods (); /*** * More All methods to traverse **/ for(FinalMethod Method:methods) { /*** See if there is an annotation label on this method **/ Finalonclick annotation = method.getannotation (onclick.class); if(Annotation! =NULL){ /*** Get the value in the note tag @OnClick (r.id.btn_main), where the value is R.id.btn_main **/ intResId =Annotation.value (); /*** Get the object **/ FinalView Btnview =Activity.findviewbyid (resId); /*** Click on the view to bind a point to listen for events **/Btnview.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (View v) {/*** Callback method that performs reflection after a click event * Anonymous inner class accesses local variables of current function, local variable declared as final type * */method.setaccessible (true); Try{method.invoke (Activity,btnview); } Catch(illegalaccessexception e) {e.printstacktrace (); } Catch(InvocationTargetException e) {e.printstacktrace (); } } }); } } } Private Static voidBindView (activity activity) {//first get all fields of activity, using reflectionClass Clazz =Activity.getclass (); //Get all the field information in the bytecodefield[] Fields =Clazz.getdeclaredfields (); //iterate over the fields to see the labels with annotations added to those fields for(Field field:fields) {viewinject annotation= Field.getannotation (viewinject.class); if(Annotation! =NULL){ /*** Get the values on the annotations **/ intResId =Annotation.value (); /*** Get current controls with activity's Findviewbyid **/View View=Activity.findviewbyid (resId); /*** Set the value of the view to the current field by reflection **/field.setaccessible (true); /*** Assign a value to the field field of the activity as a View object **/ Try{field.set (Activity,view); } Catch(illegalaccessexception e) {e.printstacktrace (); } } } }}
XML file:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/activity_main"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Im.weiyuan.com.viewutils.MainActivity"> <TextViewAndroid:id= "@+id/tv_main_title"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Hello world!" /> <ButtonAndroid:text= "Tap"Android:id= "@+id/btn_main"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>
Pretty classic.
Custom Viewutils Framework in Java