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

  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)