本版本为1.0,支持较少,使用不够方便。相关封装逻辑结构已升级至2.0,详情可参见:更完善的安卓事件监听实现

  先简单扯两句这几天学习下来对java事件监听机制的一点感触。客观地讲,java的事件监听机制相比.net好原始,暂不说委托、lamda、泛型、反射等的繁琐,仅一个事件监听,就需要各种listener才能实现,比如安卓里到处都是view.setOnXXXXListener。被C#“语法糖”和宇宙第一IDE惯坏的我真心有点不习惯,于是就决定写个工具来封装这些烦人的listener。开始切入正题。

  1. 目标
  2. 成果
  3. 实现概况
  4. 具体实现及代码
  5. 总结

一、目标

  摆脱安卓里各种listener的繁琐,像写一般的方法似的写各种事件。

二、成果

  只要写一个类(这里以MainActivityEvent命名的类为例)继承EventManager,然后在对应的MainActivity里的onCreate方法里初始化这个类(new MainActivityEvent(this))即可完成注册。剩下的就只需要在MainActivityEvent类里写对应的事件响应逻辑就可以了。

三、实现概况

  3.1 MainActivity里注册。

    

  3.2 MainActivityEvent的实现。

    

  3.3 封装的相关类型。

    

四、具体实现及代码

  4.1 EventType.java

    

package com.example.personal.events;

/**
* Event type.
*/
public enum EventType {
/**
* signature: (View v, int keyCode, KeyEvent event)
* return: boolean.
*/
OnKey,
/**
* signature: (View v, MotionEvent event)
* return: boolean.
*/
OnTouch,
/**
* signature: (View v, MotionEvent event)
* return: boolean.
*/
OnHover,
/**
* signature: (View v, MotionEvent event)
* return: boolean.
*/
OnGenericMotion,
/**
* signature: (View v)
* return: boolean.
*/
OnLongClick,
/**
* signature: (View v, DragEvent event)
* return: boolean.
*/
OnDrag,
/**
* signature: (View v, boolean hasFocus)
* return: void.
*/
OnFocusChange,
/**
* signature: (View v)
* return: void.
*/
OnClick,
/**
* signature: (ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
* return: void.
*/
OnCreateContextMenu, //TODO: Not supported for api version issues or other special reasons.
// /**
// * signature: (View v)
// */
// OnViewAttachedToWindow,
// /**
// * signature: (View v)
// */
// OnViewDetachedFromWindow,
// /**
// * signature: (View v)
// */
// OnContextClick,
// /**
// * signature: (int visibility)
// */
// OnSystemUiVisibilityChange
}

  4.2 EventAnnotation.java

    

package com.example.personal.events;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface EventAnnotation {
/**
* View id.
*
* @return the view id that the event binds to.
*/
int value(); /**
* Event type. If not specified, onClick will be set by default.
*
* @return the event type of the method binds to.
*/
EventType eventType() default EventType.OnClick;
}

  4.3 EventManager.java

    

package com.example.personal.events;

import android.app.Activity;
import android.view.ContextMenu;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; /**
* A abstract class to encapsulate most of the view event listeners. When bind an event to a view, in derived class,
* you just need to declare a method that same with the event's signature, and annotate with {@link EventAnnotation}.
* Note: the return value match is not required but recommended. For method that requires return boolean type,
* {@code true} will be returned by default if the method return type is not.
*/
public abstract class EventManager
implements
View.OnKeyListener,
View.OnTouchListener,
View.OnHoverListener,
View.OnGenericMotionListener,
View.OnLongClickListener,
View.OnDragListener,
View.OnFocusChangeListener,
View.OnClickListener,
View.OnCreateContextMenuListener { private final Map<Integer, Map<EventType, Method>> eventMap;
protected final Activity activity; protected EventManager(Activity activity) {
this.activity = activity;
eventMap = new HashMap<>();
registerEvents();
} private void setListener(View view, EventType eventType) {
switch (eventType) {
case OnKey:
view.setOnKeyListener(this);
break;
case OnTouch:
view.setOnTouchListener(this);
break;
case OnHover:
view.setOnHoverListener(this);
break;
case OnGenericMotion:
view.setOnGenericMotionListener(this);
break;
case OnLongClick:
view.setOnLongClickListener(this);
break;
case OnDrag:
view.setOnDragListener(this);
break;
case OnFocusChange:
view.setOnFocusChangeListener(this);
break;
case OnClick:
view.setOnClickListener(this);
break;
case OnCreateContextMenu:
view.setOnCreateContextMenuListener(this);
break;
}
} private void registerEvents() {
for (Method method : this.getClass().getDeclaredMethods()) {
if (method.isAnnotationPresent(EventAnnotation.class)) {
EventAnnotation annotation = method.getAnnotation(EventAnnotation.class);
int viewId = annotation.value();
View view = activity.findViewById(viewId);
if (view != null) {
method.setAccessible(true);
EventType eventType = annotation.eventType();
setListener(view, eventType);
Map<EventType, Method> actionMap;
if (eventMap.containsKey(viewId)) {
actionMap = eventMap.get(viewId);
actionMap.put(eventType, method);
} else {
actionMap = new HashMap<>();
actionMap.put(eventType, method);
eventMap.put(viewId, actionMap);
}
}
}
}
} private Object invokeAction(EventType eventType, Object... args) {
View view = null;
for (Object obj : args) {
if (obj instanceof View) {
view = (View) obj;
break;
}
} if (view == null) {
return null;
} Method action = eventMap.get(view.getId()).get(eventType);
Object result = null;
if (action != null) {
try {
result = action.invoke(this, args);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
} return result;
} private boolean invokeActionReturnStatus(EventType eventType, Object... args) {
Object result = invokeAction(eventType, args);
return result instanceof Boolean ? true : (boolean) result;
} @Override
public void onClick(View v) {
invokeAction(EventType.OnClick, v);
} @Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
invokeAction(EventType.OnCreateContextMenu, menu, v, menuInfo);
} @Override
public boolean onDrag(View v, DragEvent event) {
return invokeActionReturnStatus(EventType.OnDrag, v, event);
} @Override
public void onFocusChange(View v, boolean hasFocus) {
invokeAction(EventType.OnFocusChange, v, hasFocus);
} @Override
public boolean onGenericMotion(View v, MotionEvent event) {
return invokeActionReturnStatus(EventType.OnGenericMotion, v, event);
} @Override
public boolean onHover(View v, MotionEvent event) {
return invokeActionReturnStatus(EventType.OnHover, v, event);
} @Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
return invokeActionReturnStatus(EventType.OnKey, v, keyCode, event);
} @Override
public boolean onLongClick(View v) {
return invokeActionReturnStatus(EventType.OnLongClick, v);
} @Override
public boolean onTouch(View v, MotionEvent event) {
return invokeActionReturnStatus(EventType.OnTouch, v, event);
}
}

五、总结

  源码下载:code.rar

  为了减少重复实现listener接口的繁琐,自己写了点api以便使用。谨以此做一下记录和分享,如有问题,欢迎斧正,如果建议,欢迎反馈,此谢!

事件监听:诀别Android繁琐的事件注册机制——view.setOnXXXXListener的更多相关文章

  1. 高德地图marker事件监听-高德地图marker绑定事件就执行了[解决立即执行]

    官方的demo是这样的:地址:[http://lbs.amap.com/api/javascript-api/example/infowindow/add-infowindows-to-multipl ...

  2. Zookeeper Curator 事件监听 - 秒懂

    目录 写在前面 1.1. Curator 事件监听 1.1.1. Watcher 标准的事件处理器 1.1.2. NodeCache 节点缓存的监听 1.1.3. PathChildrenCache ...

  3. [JS]笔记12之事件机制--事件冒泡和捕获--事件监听--阻止事件传播

    -->事件冒泡和捕获-->事件监听-->阻止事件传播 一.事件冒泡和捕获 1.概念:当给子元素和父元素定义了相同的事件,比如都定义了onclick事件,点击子元素时,父元素的oncl ...

  4. 百度编辑器的内容改变事件监听bug

    先贴上我的初始化代码,可能是用法问题冤枉了百度编辑器,如果是我的用法有问题欢迎大侠们指正 <!DOCTYPE type> <html> <head> <met ...

  5. jQuery中的事件监听小记

    一,一个事件监听的简便写法 最近发现一个jQuery中事件监听的简洁写法,感觉方便好多.同时也深感自己基础薄弱,好多东西竟然都模棱两可.因此,记录的同时,也对jQuery事件监听做个小的总结 原文链接 ...

  6. Spring之事件监听(观察者模型)

    目录 Spring事件监听 一.事件监听案例 1.事件类 2.事件监听类 3.事件发布者 4.配置文件中注册 5.测试 二.Spring中事件监听分析 1. Spring中事件监听的结构 2. 核心角 ...

  7. 前端学习历程--js事件监听

    一.事件监听使用场景 1.事件触发多个方法的时候,后一个方法会把前一个方法覆盖掉. window.onload = function(){  var btn = document.getElement ...

  8. 背水一战 Windows 10 (66) - 控件(WebView): 监听和处理 WebView 的事件

    [源码下载] 背水一战 Windows 10 (66) - 控件(WebView): 监听和处理 WebView 的事件 作者:webabcd 介绍背水一战 Windows 10 之 控件(WebVi ...

  9. Spring Boot实践——事件监听

    借鉴:https://blog.csdn.net/Harry_ZH_Wang/article/details/79691994 https://blog.csdn.net/ignorewho/arti ...

随机推荐

  1. \r \r\n \t 的区别

    http://www.360doc.com/content/12/0530/15/16538_214756101.shtml \n 软回车:       在Windows 中表示换行且回到下一行的最开 ...

  2. MySQL常用命令大全(转)

    下面是我们经常会用到且非常有用的MySQL命令.下面你看到#表示在Unix命令行下执行命令,看到mysql>表示当前已经登录MySQL服务器,是在mysql客户端执行mysql命令. 登录MyS ...

  3. Mac os 上可执行jar包转app方法

    此操作可分如下几步: 1:生成jarbao: jar cf myName.jar *.class 2:生成打包所需配置文件:build.xml: <project name="MyPr ...

  4. Codeforces Round #279 (Div. 2)f

    树形最大上升子序列 这里面的上生子序列logn的地方能当模板使  good #include<iostream> #include<string.h> #include< ...

  5. HTML5画布(阴影)

    案例1: <!DOCTYPE html><html><head lang="en"> <meta charset="UTF-8& ...

  6. 窗口!窗口!- Windows程序设计(SDK)003

    窗口!窗口! 让编程改变世界 Change the world by program 内容节选: 在前边两节课的例子中,我们通过 MessageBox 函数创建一个消息框程序,消息框其实就是用来跟用户 ...

  7. Codeblocks 添加库(undefined reference 错误的处理)

    静态库  (扩展名为 .a 或 .lib) 是包含函数的文件,用于在link阶段整合执行程序,动态链接库(扩展名  .dll)是不在link阶段整合进执行程序中的. DLL文件在执行阶段动态调用 下面 ...

  8. Oracle 序列的应用

    Oracle创建序列,删除序列,得到序列 序列的创建 create sequence seq_newsId increment by 1 start with 1 maxvalue 999999999 ...

  9. QT打开网页 QURL

    用QT打开一个网页就是先定义一个QUrl对象url,然后利用QDesktopServices::open(url)即可. 例如: const QUrl url(http://www.baidu.com ...

  10. 利用GPS获取行车速度和距离

    这几天项目中需要GPS计算汽车的速度和行驶距离,这里简单记录一下使用过程 1 和平常使用地图一样,在Info.plist中添加位置请求 2 在viewdidLoad中初始化locationManage ...