EditText 焦点消失,条码扫描器读取条码时

EditText focus disappears, when the barcode scanner reads the barcode

我需要读取应用程序的条形码。我为此使用触发条形码扫描仪。通过 USB 进行通信。

如您所知,条形码扫描仪的工作方式类似于键盘。当设备读取条形码时,它会尝试将值写入具有焦点的输入。并且用户按下触发器,条码扫描器工作直到 条码读取成功。然后它自己进入待机模式。读取大量条形码的理想方式。

问题是用户按下触发器读取条码后,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中呢?

关键编辑

4年后,我发现上面的做法是完全错误的。这里有更好的方法,我不会删除旧的方法,因为它可以为其他人工作。

您应该覆盖 dispatchKeyEvent 以在将所有按键事件发送到 window 之前拦截它们。一定要为应该正常处理的关键事件调用此实现。

    String barcode = "";

    @Override
    public boolean dispatchKeyEvent(KeyEvent e)
    {
        if (e.getAction() == KeyEvent.ACTION_DOWN)
        {
            char pressedKey = (char) e.getUnicodeChar();
            barcode += pressedKey;
        }
        if (e.getAction() == KeyEvent.ACTION_UP && e.getKeyCode() == KeyEvent.KEYCODE_ENTER)
        {
            handleInput(barcode);
            barcode = "";
        }

        return false;
    }

你能试试吗:edtBarcode.setSelectAllOnFocus(true);

要隐藏键盘,您可以试试这个:Close/hide the Android Soft Keyboard

希望对您有所帮助。

基本上,当用户按下触发器时,它会触发 EditText 上的 KeyEvent。根据扫描仪的配置,可以是 KEYCODE_TABKEYCODE_ENTER

所以我所做的是听 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); // Remove this Delay Handler IF requestFocus(); works just fine without delay
                return true;
            }
            return false;
        }
    });

希望对你有所帮助~

试试这个: 在您的清单中:

<activity android:name=".Activity.TransferBtwCenterAndStoresActivity"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="stateHidden"/>

并在您的 activity class 中:

edtBarcode.setFocusableInTouchMode(true);
edtBarcode.requestFocus();

在 editText 中设置下面提到的侦听器:

edtxt.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int keyCode, KeyEvent event) {
            if (event != null && event.getAction() != KeyEvent.ACTION_DOWN) {
                return false;
            }
            if (keyCode == EditorInfo.IME_ACTION_DONE) {
                //this is for DONE button in soft keyboard is pressed
                //your logic
                return true;
            }
            if (keyCode != EditorInfo.IME_ACTION_NEXT && keyCode != EditorInfo.IME_NULL) {
                return false;
            }
            //your logic
            // this will be called when hardware enter button is pressed eg. barcode enter enter code here
            return true;
        }
    };)