我需要读取应用程序的条形码。我正在使用触发条形码扫描仪。通信是通过USB。当条形码扫描器读取条形码时,EditText焦点消失
如您所知,条形码扫描仪的工作方式与键盘类似。当设备读取条形码时,它会尝试将值写入具有焦点的输入。并且用户按下触发器,条形码扫描器将工作,直到 条形码被成功读取。然后它自己进入待机模式。阅读大量条形码的理想方式。
问题是,用户按下触发器并读取条形码后,EditText的焦点消失。焦点将随机在同一个布局中转到另一个视图。当用户试图再读取一个条形码时,操作失败,因为没有关注相关的EditText。
我试过了什么?
android:windowSoftInputMode="stateAlwaysVisible"
添加上面的行到清单文件
android:focusable="true"
android:focusableInTouchMode="true"
以上对XML侧线加。
edtBarcode.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v, boolean hasFocus)
{
if (!hasFocus)
{
//find the view that has focus and clear it.
View current = getCurrentFocus();
if (current != null)
{
current.clearFocus();
}
edtBarcode.requestFocus();
}
}
});
它检测EditText何时失去焦点。但我不能指定它。我确保EditText在触摸模式下可以聚焦。
我是如何解决它的?
handlerFocus = new Handler();
final int delay = 1000; //milliseconds
handlerFocus.postDelayed(new Runnable()
{
public void run()
{
edtBarcode.requestFocus();
handlerFocus.postDelayed(this, delay);
}
}, delay);
我知道这个解决方案不好。那么,如何在不打开键盘的情况下始终将焦点留在同一个EditText中?
什么时候的重点被改变了吗? –
用户按下触发器并且条形码读取过程成功后。 –