xx
@Override public boolean dispatchKeyEvent(KeyEvent event) { if (event.getKeyCode() == KeyEvent.KEYCODE_ENTER) { String retStr = this.getLastScanCodeString(); if (!StringUtil.isEmpty(retStr)) { //扫码数据 retStr } } return super.dispatchKeyEvent(event); }
文本框 回车、扫码
mEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { //当actionId == XX_SEND 或者 XX_DONE时都触发 //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发 //注意,这是一定要判断event != null。因为在某些输入法上会返回null。 if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_ACTION_DONE || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) { //处理事件 } return false; } });