一、概述
需求:现有一个需求,要求给Android系统接入一个数字键盘,用于方便工人操作(ps:因为触摸屏在特定场景下比键盘低效)
需要:
1.Android系统手机/pad需要能够连接数字键盘
2.Android侧能够获取到手机键盘的按键值
3.获取到键盘的按键值之后需要做对应的操作,如:模拟点击
二、使用方法
拿到按键事件:
1.Activity中
@Override public boolean dispatchKeyEvent(KeyEvent event) {//通过监听按键事件分发可以拿到 if (onSpecialKeyControl(event)) { return true; } return super.dispatchKeyEvent(event); }
2.如果页面中有EditText输入框,则最好使用view.setOnKeyListener接口
editText.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View view, int i, KeyEvent keyEvent) { onKeyCodeEvent(keyEvent, listener); return false; } });
3.数字键盘对应的值
public static boolean onKeyCodeEvent(KeyEvent keyEvent, OnSpecialKeyDownListener listener) { switch (keyEvent.getKeyCode()) { case KeyEvent.KEYCODE_NUMPAD_ADD://+号(157) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) {//如果这个判断不加上,按键会执行两次 listener.onAdd(keyEvent); } return true; case KeyEvent.KEYCODE_NUMPAD_SUBTRACT://-减号(163) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) { listener.onSubtract(keyEvent); } return true; case KeyEvent.KEYCODE_NUMPAD_DIVIDE:///除法(154) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) { listener.onDivide(keyEvent); } return true; case KeyEvent.KEYCODE_NUMPAD_MULTIPLY://*(155) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) { listener.onMultiply(keyEvent); } return true; case KeyEvent.KEYCODE_NUMPAD_COMMA://,(159) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) { listener.onComma(keyEvent); } return true; case KeyEvent.KEYCODE_NUMPAD_DOT://.(158) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) { listener.onDot(keyEvent); } return true; case KeyEvent.KEYCODE_NUMPAD_EQUALS://=(161) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) { listener.onEquals(keyEvent); } return true; case KeyEvent.KEYCODE_NUM_LOCK://指示灯(143) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) { listener.onLock(keyEvent); } return true; case KeyEvent.KEYCODE_NUMPAD_ENTER://enter(160) if (listener != null && keyEvent.getAction() != KeyEvent.ACTION_UP) { listener.onEnter(keyEvent); } return true; } return false; }
标签:return,KeyEvent,App,keyEvent,数字键盘,listener,ACTION,Android,true From: https://www.cnblogs.com/tony-yang-flutter/p/17814555.html