这篇文章主要介绍了android如何读取扫码模组数据,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
具体内容如下
android读取扫码模组数据 **在dispatchKeyEvent方法中承接读取的数据**
在dispatchKeyEvent方法中承接读取的数据
private StringBuilder mScanCodeBuilder = new StringBuilder(); @Override public boolean dispatchKeyEvent(@NonNull KeyEvent event) { LogUtil.d(TAG, "dispatchKeyEvent " + event.getAction() + ", " + event.getKeyCode()); checkLetterStatus(event);//判断大小写字母 if (event.getAction() == KeyEvent.ACTION_DOWN) { mScanCodeBuilder.append(getInputCode(event)); if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { //若为回车键,直接返回 String scanResult = mScanCodeBuilder.toString(); mScanCodeBuilder.setLength(0); LogUtil.d(TAG, "dispatchKeyEvent, " + scanResult); String scanResults = scanResult.replace(" ", ""); } else { //延迟post,若500ms内,有其他事件 mHandler.removeCallbacks(mScanningFishedRunnable); mHandler.postDelayed(mScanningFishedRunnable, 500); } return true; } return super.dispatchKeyEvent(event); }
判断大小写字母
private void checkLetterStatus(KeyEvent event) { int keyCode = event.getKeyCode(); if (keyCode == KeyEvent.KEYCODE_SHIFT_RIGHT || keyCode == KeyEvent.KEYCODE_SHIFT_LEFT) { if (event.getAction() == KeyEvent.ACTION_DOWN) { //按着shift键,表示大写 mCaps = true; } else { //松开shift键,表示小写 mCaps = false; } } }
读取字符方法
private char getInputCode(KeyEvent event) { int keyCode = event.getKeyCode(); char aChar; if (keyCode >= KeyEvent.KEYCODE_A && keyCode <= KeyEvent.KEYCODE_Z) { //字母 aChar = (char) ((mCaps ? 'A' : 'a') + keyCode - KeyEvent.KEYCODE_A); } else if (keyCode >= KeyEvent.KEYCODE_0 && keyCode <= KeyEvent.KEYCODE_9) { //数字 aChar = (char) ('0' + keyCode - KeyEvent.KEYCODE_0); } else { //符号 switch (keyCode) { case KeyEvent.KEYCODE_GRAVE: aChar = '`'; break; case KeyEvent.KEYCODE_COMMA: aChar = ','; break; case KeyEvent.KEYCODE_APOSTROPHE: aChar = '\''; break; case KeyEvent.KEYCODE_POUND: aChar = '#'; break; case KeyEvent.KEYCODE_STAR: aChar = '*'; break; case KeyEvent.KEYCODE_PERIOD: aChar = '.'; break; case KeyEvent.KEYCODE_MINUS: aChar = mCaps ? '_' : '-'; break; case KeyEvent.KEYCODE_SLASH: aChar = '/'; break; case KeyEvent.KEYCODE_BACKSLASH: aChar = mCaps ? '|' : '\\'; break; case KeyEvent.KEYCODE_EQUALS: aChar = '='; break; case KeyEvent.KEYCODE_SEMICOLON: aChar = ';'; break; case KeyEvent.KEYCODE_PLUS: aChar = '+'; break; case KeyEvent.KEYCODE_AT: aChar = '@'; break; case KeyEvent.KEYCODE_LEFT_BRACKET: aChar = '['; break; case KeyEvent.KEYCODE_RIGHT_BRACKET: aChar = ']'; break; default: aChar = ' '; break; } } Log.d(TAG, "char" + aChar); return aChar; }
延迟500毫秒相关
private Handler mHandler = new Handler(this); private final Runnable mScanningFishedRunnable=new Runnable() { @Override public void run() { } };
感谢你能够认真阅读完这篇文章,希望小编分享的“android如何读取扫码模组数据”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。