您好,登錄后才能下訂單哦!
這篇文章主要介紹了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。