当条形码扫描器读取条形码时,EditText焦点消失 [英] EditText focus disappears, when the barcode scanner reads the barcode

查看:265
本文介绍了当条形码扫描器读取条形码时,EditText焦点消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读一个应用程序的条形码。我正在使用触发条形码扫描仪。通信是通过USB。



正如你所知道的,条码扫描器的工作方式就像键盘一样。当设备读取条形码时,它会尝试将值写入具有焦点的输入。并且用户按下触发器,条形码扫描器工作,直到
条形码被成功读取。然后它自己处于待机模式。阅读大量条形码的理想方法。

问题在于用户按下触发器并读取条形码后,EditText的焦点消失。焦点将随机在相同的布局中转到另一个视图。当用户试图再读一个条形码时,操作失败,因为没有关注相关的EditText。



我尝试了什么?

  android:windowSoftInputMode =stateAlwaysVisible

添加到清单文件的上面一行

$ p $ android:focusable =true
android:focusableInTouchMode =true

在xml一侧添加上面几行。

  edtBarcode.setOnFocusChangeListener(new View.OnFocusChangeListener()
{
@Override
public void onFocusChange(View v,boolean hasFocus)
{
if(!hasFocus)
{
//找到具有焦点的视图并清除它
查看当前= getCurrentFocus();
if(current!= null)
{
current.clearFocus();
}

edtBarcode.requestFocus();
}
}
});

它检测EditText何时失去焦点。但是我不能把它归还。我确保EditText在触摸模式下是可以关注的。

我是如何解决它的?

  handlerFocus = new Handler(); 
final int delay = 1000; //毫秒

handlerFocus.postDelayed(new Runnable()
{
public void run()
{
edtBarcode.requestFocus();
handlerFocus.postDelayed(this,delay);
}
},delay);

我知道这个解决方案不好。那么如何让焦点总是停留在同一个EditText而不打开键盘?基本上当用户按下触发器时,它会触发 EditText 上的 KeyEvent 。根据扫描仪的配置,可以是 KEYCODE_TAB KEYCODE_ENTER


$ b $所以我所做的就是听取 OnKeyEvent 而不是 OnFocusChange



试试这个:

  edtBarcode.setOnKeyListener(new View.OnKeyListener(){
@Override
public boolean onKey(View v,int keyCode,KeyEvent event){
if((event.getAction()== KeyEvent.ACTION_UP& keyCode == KeyEvent.KEYCODE_ENTER)
|| keyCode == KeyEvent.KEYCODE_TAB){
// handleInputScan();
new Handler()。postDelayed(new Runnable(){
@Override
public void run(){
if(edtBarcode!= null){
edtBarcode.requestFocus();
}
}
},10); //删除这个Delay Handler IF requestFocus();工作很好,没有延迟
返回true;
}
返回false;
}
});

希望这有助于〜


I need to read a barcode for an application. I am using trigger barcode scanner for this. Communication is via USB.

As you know, barcode scanners work like keyboards. When the device reads the barcode, it tries to write the value to the input that has focus. And the user presses the trigger, the barcode scanner works until the barcode is read successfully. Then it takes itself in standby mode. Ideal way to read a lot of barcodes.

The problem is that after the user presses the trigger and reads the barcode, the focus of the EditText disappears. Focus is going to another view in the same layout randomly. When the user tries to read one more barcode, the operation fails because there is no focus on related EditText.

What did I try?

android:windowSoftInputMode="stateAlwaysVisible"

Added above line to the manifest file

android:focusable="true"
android:focusableInTouchMode="true"

Added above lines on the xml side.

 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();
            }
        }
    });

It detects when EditText loses its focus. But i can't assign it back. I ensure that the EditText is focusable in touch mode.

How did i solve it?

    handlerFocus = new Handler();
    final int delay = 1000; //milliseconds

    handlerFocus.postDelayed(new Runnable()
    {
        public void run()
        {
            edtBarcode.requestFocus();
            handlerFocus.postDelayed(this, delay);
        }
    }, delay);

I know this solution is not good. So how do I make the focus always stay in the same EditText without opening the keyboard?

解决方案

Basically when user presses the trigger, it triggers the KeyEvent on your EditText. Which can be KEYCODE_TAB or KEYCODE_ENTER, based on the scanner's configuration.

So what I did is to listen to the OnKeyEvent rather than the OnFocusChange.

Try this:

edtBarcode.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if ((event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_ENTER)
                    || keyCode == KeyEvent.KEYCODE_TAB) {
                // handleInputScan();
                new Handler().postDelayed(new Runnable() {
                      @Override
                      public void run() {
                          if (edtBarcode != null) {
                               edtBarcode.requestFocus();
                          }
                      }
                }, 10); // Remove this Delay Handler IF requestFocus(); works just fine without delay
                return true;
            }
            return false;
        }
    });

Hope this helps~

这篇关于当条形码扫描器读取条形码时,EditText焦点消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆