android 设备对接扫码枪,切换输入设备弹出键盘问题
禁用弹出键盘,避免出现第一个字符漏输入的问题
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
用 addTextChangedListener 监听扫码枪的数据
edittext.addTextChangedListener(new TextWatcher() {}
然后用 setOnEditorActionListener 监听回车, 回车时的事件来触发业务行为
edittext.setOnEditorActionListener { v, actionId, event ->
if (event.keyCode == KeyEvent.KEYCODE_ENTER) {
// 业务逻辑
}
false
}
扫到的码最后⼀般带有回车符,导致 EditText 失去焦点。可给 EditText 配置如下 next 系列属性,使其下个焦点仍然指向⾃⼰
<EditText
id="@+id/scan_code"
layout_width="300px"
layout_height="100px"
nextFocusDown="@id/scan_code"
nextFocusForward="@id/scan_code"
nextFocusLeft="@id/scan_code"
nextFocusRight="@id/scan_code"
nextFocusUp="@id/scan_code"
nextClusterForward="@id/scan_code"/>