亲宝软件园·资讯

展开

android读取扫码模组数据 android读取扫码模组数据的方法

虾皮条 人气:0
想了解android读取扫码模组数据的方法的相关内容吗,虾皮条在本文为您仔细讲解android读取扫码模组数据的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android读取扫码模组数据,android读取扫码数据,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() {

        }
    };

加载全部内容

相关教程
猜你喜欢
用户评论