安卓使用外接扫码枪扫描一维码之后EditText失去焦点的问题

使用EditText获取扫码枪的扫描结果,但是不同的扫码枪却有不同表现,部分扫码枪能正常工作,而有些扫码枪在扫描之后,会自动把焦点指向下一个控件,仿佛在windows的桌面上按Tab键一样,扫一下就定位下一个控件,这不是我们想要的效果,我们希望扫描之后,焦点仍然停留在EditText上。
解决方法:使用nextFocus属性指向EditText本身就可以了:注意xml最后的配置
android:nextFocusDown=”@id/et_input_code”
android:nextFocusForward=”@id/et_input_code”
android:nextFocusRight=”@id/et_input_code”
android:nextFocusLeft=”@id/et_input_code”
android:nextFocusUp=”@id/et_input_code”
android:nextClusterForward=”@id/et_input_code”

<EditText
                android:id="@+id/et_input_code"
                android:layout_width="match_parent"
                android:layout_height="@dimen/height_input_code"
                android:background="@color/white"
                android:hint="Please Input Code"
                android:singleLine="true"
                android:longClickable="false"
                android:paddingLeft="@dimen/padding_default"
                android:textColor="@color/black_text"
                android:textColorHint="@color/gray_text"
                android:textSize="@dimen/text_size_middle"
                android:nextFocusDown="@id/et_input_code"
                android:nextFocusForward="@id/et_input_code"
                android:nextFocusRight="@id/et_input_code"
                android:nextFocusLeft="@id/et_input_code"
                android:nextFocusUp="@id/et_input_code"
                android:nextClusterForward="@id/et_input_code"/>
@Override
    public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
        // scanned a product code
        if (keyEvent != null && keyEvent.getKeyCode() == KeyEvent.KEYCODE_ENTER && keyEvent.getAction() == KeyEvent.ACTION_UP) {
            Product scannedProduct = ProductManager.getInstance().getProductFromCode(et_input_code.getText().toString());
            onProductsChange(scannedProduct, QUANTITY_ADD);
            et_input_code.setText("");
        }

        return true;
    }

猜你喜欢

转载自blog.csdn.net/ithouse/article/details/79026952