Android Annotation使用快速入门,Android入门

第一步打开Android studio定义接口

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

@Target(ElementType.METHOD)

@Retention(RetentionPolicy.RUNTIME)

public @interface ActionListener {

public String source() default “nothing”;

}

插图:

Android Annotation使用快速入门,Android入门

@Target:注解的作用目标

@Target说明了Annotation所修饰的对象范围:Annotation可被用于packages、types(类、接口、枚举、Annotation类型)、类型成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数)。

@Retention:注解的保留位置

@Retention定义了该Annotation被保留的时间长短:某些Annotation仅出现在源代码中,而被编译器丢弃;而另一些却被编译在class文件中;编译在class文件中的Annotation可能会被虚拟机忽略,而另一些在class被装载时将被读取(请注意并不影响class的执行,因为Annotation与class在使用上是被分离的)。

2.*注解事件处理器

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

编写注解事件处理器,通过反射获得场景类对象的annotation的source对象。并且为source对象调用setOnClickListener方法进行事件绑定。

第二步:对对象进行事件绑定,添加监听事件

import android.view.View;

import java.lang.reflect.Field;

import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;

public class ActionListenerInstaller {

public static void processAnnotations(Object client) {

Class<?> client = client.getClass();

for (Method m : client.getDeclaredMethods()) {

//获取指定Annotation对象

ActionListener listener = m.getAnnotation(ActionListener.class);

if (listener != null) {

try {

Field f = client.getDeclaredField(listener.source());

f.setAccessible(true);

//控件对象

Object focusView = f.get(client);

//addListenr函数添加监听,当click事件发生时,调用 onBtnClick() 函数

addListenr(focusView, client, m);

} catch (Exception e) {

e.printStackTrace();

}

}

}

}

private static void addListenr(final Object focusView, final Object client, final Method m) throws Exception {

InvocationHandler handler = new InvocationHandler() {

@Override

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

//场景类调用 onBtnClick() 方法

return m.invoke(client);

}

};

Object onClickListenr = Proxy.newProxyInstance(null, new Class[]{View.OnClickListener.class}, handler);

Method setOnClickListenerMethod = focusView.getClass().getMethod(“setOnClickListener”, View.OnClickListener.class);

setOnClickListenerMethod.invoke(focusView, onClickListenr);

}

}

插图:

Android Annotation使用快速入门,Android入门

3.注解应用场景类

第三步:注解应用场景类中,只需要调用处理器中的processAnnotations方法,通过反射完成click事件的绑定,同时为click事件指定source控件,添加事件处理函数onBtnClick。这样一个简单的Android注解应用就完成了。

import android.app.Activity;

import android.os.Bundle;

import android.widget.Button;

public class MainActivity extends Activity {

private Button test_btn;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

test_btn = (Button) findViewById(R.id.test_btn);

ActionListenerInstaller.processAnnotations(this);

}

@ActionListener(source = “test_btn”)

public void onBtnClick() {

android.util.Log.d(“czh”,“CLICK 事件发生了”);

}

}

插图:

Android Annotation使用快速入门,Android入门

运行结果插图:

Android Annotation使用快速入门,Android入门

环境配置

使用Android注解前需要导入相关的包:

compile ‘com.android.support:support-annotations:latest.integration’

上一篇:注解(Annotation)


下一篇:Modelica建模方式2--基于组件建模和基于子系统建模