compile 'com.jakewharton:butterknife:8.8.1' annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮1" /> <Button android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮2" /> <Button android:id="@+id/bt3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮3" /> <Button android:id="@+id/bt4" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="按钮4" /> </LinearLayout>
IBindView.java
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Created by lenovo on 2017/11/28. */ @Documented @Target(ElementType.FIELD)//作用域 @Retention(RetentionPolicy.RUNTIME)//运行时在作用 public @interface IBindView { int value(); }
IOnClick.java
import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * Created by lenovo on 2017/11/28. */ @Documented @Target(ElementType.METHOD)//方法 @Retention(RetentionPolicy.RUNTIME) public @interface IOnClick { int value(); }
MainActivity.java
package panjiangang.bwie.com.lianxi1_day02; import android.app.Activity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Main2Activity extends AppCompatActivity { @IBindView(R.id.bt1) public Button bt1; @IBindView(R.id.bt2) public Button bt2; @IBindView(R.id.bt3) public Button bt3; @IBindView(R.id.bt4) public Button bt4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initId(this); // initClick(this);//有监听事件的方法 bt1.setText("A"); bt2.setText("B"); bt3.setText("C"); bt4.setText("D"); } public static void initId(Activity activity) { Class clazz = activity.getClass(); //获取当前类的所有属性 Field[] fields = clazz.getFields();//所有公开的字段 (属性) // 遍历属性 for (Field field : fields) { // 判断当前属性 身上是否有IBindView 注解 if (field.isAnnotationPresent(IBindView.class)) { // 获取当前属性身上的注解 IBindView iBindView = field.getAnnotation(IBindView.class); //取出注解 对应的值 int id = iBindView.value(); //查找控件 View view = activity.findViewById(id); try { // 把view 设置给当前的属性 field.set(activity, view); } catch (IllegalAccessException e) { e.printStackTrace(); } } } } public static void initClick(final Activity activity) { Class clazz = activity.getClass(); Field[] fields = clazz.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(IBindView.class)) { IBindView bindView = field.getAnnotation(IBindView.class); View view = activity.findViewById(bindView.value()); try { field.set(activity, view); // 获取当前类的public 方法 Method[] methods = clazz.getMethods(); for (final Method method : methods) { //判断当前方法上是否有IOnClick 注解 if (method.isAnnotationPresent(IOnClick.class)) { //获取方法上的注解 IOnClick click = method.getAnnotation(IOnClick.class); // 获取注解值 int id = click.value(); //点击事件出发条件 if (bindView.value() == id) { view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { // 调用点击事件方法 method.invoke(activity); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } } }); } } } } catch (IllegalAccessException e) { e.printStackTrace(); } } } } @IOnClick(R.id.bt4) public void onViewClicked() { Toast.makeText(this, "bt4", Toast.LENGTH_SHORT).show(); } }