吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1675|回复: 5
收起左侧

[其他原创] J8AutoBindView 让人再懒一点

  [复制链接]
djwdj 发表于 2021-6-10 10:26
废话不多说,先上代码
[Java] 纯文本查看 复制代码
 @J8BindView(R.id.bt)
    Button bt;


[Java] 纯文本查看 复制代码
Field[] declaredFields = activity.getClass().getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field field = declaredFields[i];
            field.setAccessible(true);
            J8BindView bv;
            if ((bv = field.getAnnotation(J8BindView.class))!=null) {
                int id = bv.value();
                View view = activity.findViewById(id);
                try {
                    field.set(activity, view);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

这是旧的写法,要写R.id,太累了。所以就要个自动的
[Java] 纯文本查看 复制代码
@J8AutoBindView
TextView tv;
@J8AutoBindView
EditText et;

[Java] 纯文本查看 复制代码
Field[] declaredFields = activity.getClass().getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field field = declaredFields[i];
            field.setAccessible(true);
            J8AutoBindView abv = field.getAnnotation(J8AutoBindView.class);
            if (abv != null) {
                int id = activity.getResources().getIdentifier(field.getName(), "id", activity.getPackageName());
                View view = activity.findViewById(id);
                try {
                    field.set(activity, view);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

使用和id同名的变量就能自动了
image.png

setContentView之后
[Java] 纯文本查看 复制代码
J8AutoBindViewUtils.bind(this);


完整J8AutoBindViewUtils
[Java] 纯文本查看 复制代码
package djwdj.utils;

import android.app.Activity;
import android.view.View;

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

import djwdj.annon.J8AutoBindOnClick;
import djwdj.annon.J8AutoBindOnLongClick;
import djwdj.annon.J8AutoBindOnTouch;
import djwdj.annon.J8AutoBindView;
import djwdj.annon.J8BindOnClick;
import djwdj.annon.J8BindOnLongClick;
import djwdj.annon.J8BindOnTouch;
import djwdj.annon.J8BindView;

public class J8AutoBindViewUtils {
    public static void bind(final Activity activity){
        //反射属性
        Field[] declaredFields = activity.getClass().getDeclaredFields();
        for (int i = 0; i < declaredFields.length; i++) {
            Field field = declaredFields[i];
            field.setAccessible(true);
            J8AutoBindView abv = field.getAnnotation(J8AutoBindView.class);
            J8BindView bv;
            if (abv != null) {
                int id = activity.getResources().getIdentifier(field.getName(), "id", activity.getPackageName());
                View view = activity.findViewById(id);
                try {
                    field.set(activity, view);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }else
            if ((bv = field.getAnnotation(J8BindView.class))!=null) {
                int id = bv.value();
                View view = activity.findViewById(id);
                try {
                    field.set(activity, view);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        //反射方法
        Method[] declaredMethods = activity.getClass().getDeclaredMethods();
        for (int i = 0; i < declaredMethods.length; i++) {
            final Method method = declaredMethods[i];
            method.setAccessible(true);
            J8AutoBindOnClick abc;
            J8BindOnClick bc = method.getAnnotation(J8BindOnClick.class);
            if (bc!=null) {
                int[] value = bc.value();
                for (int id : value) {
                    final View btn = activity.findViewById(id);
                    btn.setOnClickListener(new J8OnClick(method,activity,btn));
                }
            }else
                if((abc=method.getAnnotation(J8AutoBindOnClick.class))!=null){
                    int id = activity.getResources().getIdentifier(method.getName(), "id", activity.getPackageName());
                    final View btn = activity.findViewById(id);
                    btn.setOnClickListener(new J8OnClick(method,activity,btn));
                }
            J8AutoBindOnLongClick ablc;
            J8BindOnLongClick blc = method.getAnnotation(J8BindOnLongClick.class);
            if (blc!=null) {
                int[] value = blc.value();
                for (int id : value) {
                    final View btn = activity.findViewById(id);
                    btn.setOnLongClickListener(new J8OnLongClick(method,activity,btn));
                }
            }else
                if((ablc=method.getAnnotation(J8AutoBindOnLongClick.class))!=null){
                    int id = activity.getResources().getIdentifier(method.getName(), "id", activity.getPackageName());
                    final View btn = activity.findViewById(id);
                    btn.setOnLongClickListener(new J8OnLongClick(method,activity,btn));
                }
            J8AutoBindOnTouch abt;
            J8BindOnTouch bt = method.getAnnotation(J8BindOnTouch.class);
            if (bt!=null) {
                int[] value = bt.value();
                for (int id : value) {
                    final View btn = activity.findViewById(id);
                    btn.setOnTouchListener(new J8OnTouch(method,activity,btn));
                }
            }else
                if((abt=method.getAnnotation(J8AutoBindOnTouch.class))!=null){
                    int id = activity.getResources().getIdentifier(method.getName(), "id", activity.getPackageName());
                    final View btn = activity.findViewById(id);
                    btn.setOnTouchListener(new J8OnTouch(method,activity,btn));
                }
        }
    }
}


[Java] 纯文本查看 复制代码
public class J8OnClick  implements View.OnClickListener {
    Object[] o;
    public J8OnClick(Object... o){
        this.o=o;
    }
    @Override
    public void onClick(View v) {

        try {
//            method.invoke(activity, btn);
            ((Method)o[0]).invoke(o[1], o[2]);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

[Java] 纯文本查看 复制代码
public class J8OnLongClick implements View.OnLongClickListener {
    Object[] o;
    public J8OnLongClick(Object... o){
        this.o=o;
    }

    @Override
    public boolean onLongClick(View v) {
        try {
            Method m= (Method) o[0];
            Object d = m.invoke(o[1], o[2]);
            return (boolean) d;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

[Java] 纯文本查看 复制代码
public class J8OnTouch implements View.OnTouchListener {
    Object[] o;
    public J8OnTouch(Object... o){
        this.o=o;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        L.d(event.getAction());
        try {
            Method m= (Method) o[0];
            return (boolean) m.invoke(o[1], o[2],event);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}

注释器部分的代码
[Java] 纯文本查看 复制代码
@Retention(RetentionPolicy.RUNTIME)
public [url=home.php?mod=space&uid=593715]@interface[/url] J8AutoBindView {
}

[Java] 纯文本查看 复制代码
@Retention(RetentionPolicy.RUNTIME)
public @interface J8AutoBindOnClick {
}

[Java] 纯文本查看 复制代码
@Retention(RetentionPolicy.RUNTIME)
public @interface J8AutoBindOnLongClick {
}

[Java] 纯文本查看 复制代码
@Retention(RetentionPolicy.RUNTIME)public @interface J8AutoBindOnTouch {
}


[Java] 纯文本查看 复制代码
@Retention(RetentionPolicy.RUNTIME)
//@Target(ElementType.FIELD)
public @interface J8BindView {
    int value();
}

[Java] 纯文本查看 复制代码
@Retention(RetentionPolicy.RUNTIME)
//@Target(ElementType.METHOD)
public @interface J8BindOnClick {
    int[] value();
}

[Java] 纯文本查看 复制代码
@Retention(RetentionPolicy.RUNTIME)
public @interface J8BindOnLongClick {
    int[] value();
}

[Java] 纯文本查看 复制代码
@Retention(RetentionPolicy.RUNTIME)
public @interface J8BindOnTouch {
    int[] value();
}

没写@Target,需要的自己动手

J8AutoBindViev.zip

148.86 KB, 下载次数: 3, 下载积分: 吾爱币 -1 CB

DEMO

免费评分

参与人数 3吾爱币 +12 热心值 +3 收起 理由
笙若 + 1 + 1 谢谢@Thanks!
苏紫方璇 + 10 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!
461735945 + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| djwdj 发表于 2021-6-10 10:33
我没有蓝凑,路过的基友可以顺手搞一个

点评

https://fletime.lanzoui.com/iCiJwq0321c  详情 回复 发表于 2021-6-10 10:52
宜城小站 发表于 2021-6-10 10:51
FleTime 发表于 2021-6-10 10:52
wujl82 发表于 2021-6-10 10:57
有这个工具之后确实可以让我更懒了,谢谢分享
Lwk520pj 发表于 2021-6-11 17:22
没看明白可以实现什么功能
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 15:25

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表