废话不多说,先上代码[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同名的变量就能自动了
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,需要的自己动手
|