美文网首页扫码枪相关
使用扫码枪输入字符的总结

使用扫码枪输入字符的总结

作者: 落英坠露 | 来源:发表于2018-03-14 09:34 被阅读430次

    项目中的要使用扫码枪录入商品条码,读取微信、支付宝的付款码。在调试的过程中,遇到一些问题,在这里做个总结。

    扫码枪使即插即用的输入设备,不需要软件驱动,PC、Android 机器都可以使用。它读取一串数字或者字母,然后以回车键结束,用文本表示就是这样的 1234567890\n

    要实现的功能是,一直不断地扫码输入,扫完一个继续下一个,类似于超市里面的收银结账。下次输入的时候要清空之前的字符,如何判读输入的开始是关键,但是这个没有一个标准。考虑过后,我用一个输入框的「隐形替身」来完成聚焦,读到字符串后用另外的 EditText 显示。试过之后发现它完美解决了我的问题。

    来看代码:

    public class BarcodeInputWatcher implements View.OnFocusChangeListener, TextWatcher, TextView.OnEditorActionListener {
        private final ILogger logger = LoggerFactory.getLogger(BarcodeInputWatcher.class);
        private static final long ONE_MILLION = 1000000;
        /**
         * 判定扫描枪输入的最小间隔,模拟器3000毫秒,真机300毫秒
         */
        private static final int BARCODE_INPUT_INTERVAL = 300;
        // 开始输入的时刻
        private long mBeginning;
        // 扫码监听器
        private OnBarcodeInputListener mOnBarcodeInputListener;
        // 替身 EditText,通过构造方法传入
        private EditText mEditText;
    
        public BarcodeInputWatcher(EditText editText) {
            editText.setOnEditorActionListener(this);
            editText.setOnFocusChangeListener(this);
            editText.addTextChangedListener(this);
            mEditText = editText;
        }
    
        public void setOnBarcodeInputListener(OnBarcodeInputListener onBarcodeInputListener) {
            mOnBarcodeInputListener = onBarcodeInputListener;
        }
    
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            // 监听回车键. 这里注意要作判断处理,ActionDown、ActionUp 都会回调到这里,不作处理的话就会调用两次
            boolean isEnter = event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN;
            if (isEnter || actionId == EditorInfo.IME_ACTION_DONE) {
                long duration = (System.nanoTime() - mBeginning) / ONE_MILLION;
                String text = v.getText();
                logger.info("点击了 Enter. 输入的字符:{} 耗时{}ms", text, duration);
                if (duration < BARCODE_INPUT_INTERVAL) {
                    if (mOnBarcodeInputListener != null) {
                        mOnBarcodeInputListener.onBarcodeInput(text);
                    }
                }
                mBeginning = 0;
                mEditText.setText("");
                // 如果返回 false,当输入字符回车时,会自动调用输入框的点击事件
                return true;
            } else {
                return false;
            }
        }
    
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            logger.verbose("onFocusChange. hasFocus:{}", hasFocus);
        }
    
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            logger.verbose("beforeTextChanged. s:{}, start:{}, count:{}, after:{}", s, start, count, after);
            // 重新输入时,重置计时器
            if (TextUtils.isEmpty(s) || start == 0) {
                mBeginning = System.nanoTime();
            }
        }
    
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            logger.verbose("onTextChanged. s:{}, start:{}, before:{}, count:{}", s, start, before, count);
        }
    
        @Override
        public void afterTextChanged(Editable s) {
            logger.verbose("afterTextChanged. s:{}", s);
        }
    
        /**
         * 扫码输入监听器
         */
        public interface OnBarcodeInputListener {
            /**
             * 扫码输入完成
             *
             * @param barcode 输入的条码
             */
            void onBarcodeInput(String barcode);
        }
    }
    

    「替身」EditText,指定 1 像素高度,透明的背景和文本,在布局文件中,放在显示条码的 TextView 的旁边。(反正看不见,可以随便放,开玩笑的 ^_^)

    <EditText
        android:id="@+id/et_barcode_fake"
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:background="@color/transparent"
        android:cursorVisible="false"
        android:imeOptions="actionDone"
        android:inputType="text"
        android:maxLength="20"
        android:textColor="@color/transparent" />
    

    使用时进行初始化,传入要操作的 EditText 和回调接口,并让扫码枪获取焦点。

            EditText mEtBarcodeFake = (EditText) findViewById(R.id.et_barcode_fake);
            
            BarcodeInputWatcher barcodeInputWatcher = new BarcodeInputWatcher(mEtBarcodeFake);
            barcodeInputWatcher.setOnBarcodeInputListener(new BarcodeInputWatcher.OnBarcodeInputListener() {
                @Override
                public void onBarcodeInput(String barcode) {
                    // 处理输入的条码,用 TextView 显示出来
                }
            });
    
            mEtBarcodeFake.requestFocus();
            mEtBarcodeFake.requestFocusFromTouch();
    

    到这儿,扫码枪的输入已经可以由我们控制,业务也可以顺利进行下去啦。

    回过头来看,这种方法和代理模式很像,显示字符的 EditText 是被代理对象,把接受输入的操作交给代理来做;「替身」EditText 是代理对象,输入字符后交给被代理者显示。一个是显性的,一个是隐性的,搭配在一起就可以实现功能。

    相关文章

      网友评论

        本文标题:使用扫码枪输入字符的总结

        本文链接:https://www.haomeiwen.com/subject/dlixqftx.html