续接上一篇文章
public class MainActivity extends AppCompatActivity {
//上面这个测试,用来注解str
@TestRentention("呵呵呵")
String str;
@AutoObject
Student student;
@AutoObject
MyData myData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//功能是:如何得到 @TestRetention("memeda")
//getZhujieConent();
ZhuJieBind.bind(this);
student.getInfo("zz");
Toast.makeText(this, student.toString(), Toast.LENGTH_SHORT).show();
myData.setData("lf", "最帅,super start");
Toast.makeText(this, myData.toString(), Toast.LENGTH_SHORT).show();
}
private void getZhujieConent() {
MainActivity mainActivity = new MainActivity();
Class mClass = mainActivity.getClass();
Field[] fields = mClass.getDeclaredFields();
for (Field f : fields) {
TestRentention annotation = f.getAnnotation(TestRentention.class);
if (annotation != null) {
String value = annotation.value();
Log.e("value", value);
}
}
}
}
写了两种方法一种是直接在MainActivity中实现,另一种是写了两个工具类
顺便再对注解进行一下说明
1.元注解?约束注解的注解 给你提供些的格式
2.如何写一个注解类
新写一个类把类的修饰符改成@interface
Retention 这个注解再什么时候能作用到
Target 规定注解作用到成员变量或者成员方法
/**
* FIELD:领域 ElementType元素类型,对象类型 RetentionPolicy:定义在
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TestRentention {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface AutoObject {
}
点击事件运行触发的注解
public class BindDemoActivity extends AppCompatActivity implements View.OnClickListener {
@BindView(R.id.tv_one)
public TextView tv_one;
@BindView(R.id.bt_two)
public Button bt_two;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bind_demo);
initView();
}
private void initView() {
tv_one = (TextView) findViewById(R.id.tv_one);
bt_two = (Button) findViewById(R.id.bt_two);
bt_two.setOnClickListener(this);
tv_one.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt_two:
Toast.makeText(this, "牛逼的不行", Toast.LENGTH_SHORT).show();
break;
case R.id.tv_one:
Toast.makeText(this, "卧槽,卧槽", Toast.LENGTH_SHORT).show();
break;
}
}
}
工具类注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface BindView {
//
int value() default 123;
}
/*METHOD方法,类函数*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface ClickView {
int value();
}
两个注解包都需要用到的工具类
/**
* 自动创建对象
* 1:想要创建对象就要newInstance方法
*/
public class ZhuJieBind {
//MainActivity创建对象
public static void bind(Object object) {
Class mClass = object.getClass();
Field[] fields = mClass.getDeclaredFields();
//给MainActivity里面的一个成员创建变量
for (Field f : fields) {
//这个方法就是判断这个变量有没有加参数里面的注解
AutoObject annotation = f.getAnnotation(AutoObject.class);
if (annotation != null) {
//此处必须加上暴力注解方法 fouz会报错
f.setAccessible(true);
try {
//找到这个变量 因为反射机制提供了一个方法让你通过成员变量就可以获取到一个字节码对象
Class mtype = f.getType();
Constructor constructor = mtype.getConstructor();
//给MainActivity里面的一个成员变量赋值
f.set(object, constructor.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
//初始化组件+点击事件
public static void bindView(final Activity activity) {
Class mClass = activity.getClass();
Field[] fields = mClass.getDeclaredFields();
Method[] methods = mClass.getDeclaredMethods();
for (Field f : fields) {
//遵循java运行规律
BindView annotation = f.getAnnotation(BindView.class);
if (annotation != null) {
View viewById = activity.findViewById(annotation.value());
try {
f.set(activity, viewById);
} catch (Exception e) {
e.printStackTrace();
}
}
for (final Method m : methods) {
ClickView clickView = m.getAnnotation(ClickView.class);
if (clickView != null && clickView.value() != 0) {
final View viewById = activity.findViewById(clickView.value());
viewById.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
m.invoke(activity,viewById);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
});
}
}
}
}
}
自己的想法以及理解都在代码的注释中写上了,如果谁有新的不同的理解可以告诉我,让我在代码的道路上走的更深更远.