欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android外接扫码枪监听普通输入事件

程序员文章站 2022-06-14 22:24:42
...
  String barcode ="";
    @Override
    public boolean dispatchKeyEvent(KeyEvent event) {
        if(event.getAction()==KeyEvent.ACTION_DOWN){
            Log.e("123123","dispatchKeyEvent: "+event.toString());
            char pressedKey = (char) event.getUnicodeChar();
            barcode += pressedKey;
        }
        if (event.getAction()==KeyEvent.ACTION_DOWN && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) {
            Toast.makeText(getApplicationContext(),
                    "barcode--->>>" + barcode, Toast.LENGTH_LONG)
                    .show();
            Log.i("123123",barcode);
          //  Toast.makeText(MainActivity.this,barcode,Toast.LENGTH_LONG).show();
            barcode="";
        }
        return super.dispatchKeyEvent(event);
    }//监听二维码数据

重写dispatchKeyEvent方法即可

相关标签: Android 安卓