自定义注解,仿butterknife,找控件点击事件

activity_butter.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"
    tools:context="com.example.menglucywhh.day1128_create.butterknife.ButterActivity">

    <Button
        android:text="button1"
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="button2"
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="button3"
        android:id="@+id/button3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <Button
        android:text="button4"
        android:id="@+id/button4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>
自定义注解的类IBindView

@Documented
@Target(ElementType.FIELD)//在属性上注解
@Retention(RetentionPolicy.RUNTIME)
//自己写的butterknife
public @interface IBindView {
    int value();
}
自定义注解的类,点击事件.IOnclick
@Documented
@Target(ElementType.METHOD)//在方法上注解
@Retention(RetentionPolicy.RUNTIME)
public @interface IOnclick {
    int value();
}
ButterActivity.java里面通过反射自定义注解

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 com.example.menglucywhh.day1128_create.R;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ButterActivity extends AppCompatActivity {

    @IBindView(R.id.button1)
    Button button1;
    @IBindView(R.id.button2)
    Button button2;
    @IBindView(R.id.button3)
    Button button3;
    @IBindView(R.id.button4)
    Button button4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_butter);

        //调用找到 id和设置点击事件的方法
        initClick(this);
        button2.setText("按钮1");
        button3.setText("按钮2");

    }

    //找id和设置点击事件的方法
    private static void initClick(final Activity activity) {
        //获取到当前的类
        Class clazz = activity.getClass();

        //获取当前类的所有属性 ,
        Field[] fields = clazz.getDeclaredFields();

        //遍历属性
        for (Field field:fields){
            //判断当前属性 身上是否有ibindview注解
            if(field.isAnnotationPresent(IBindView.class)){
                //获取当前属性身上的注解
                IBindView iBindView = field.getAnnotation(IBindView.class);
                //查找控件,根据这个id查出控件
                View view = activity.findViewById(iBindView.value());

                //把view设置给当前的属性
                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();
                            //如果点击事件上面的注解id和 上面的id一样.,触发点击事件
                            if(id == iBindView.value()){
                                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.button2)
    public void onViewClicked(){
        Toast.makeText(this, "button2", Toast.LENGTH_SHORT).show();
    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值