The problem of EditText losing focus after Android uses external code scanner to scan one-dimensional code

Use EditText to get the scanning results of the code scanning gun, but different code scanning guns have different performances. Some code scanning guns can work normally, and some code scanning guns will automatically focus on the next control after scanning, as if pressing Tab on the desktop of windows, scanning will locate the next control, which is not the effect we want. We want to focus after scanning The point remains on EditText.
Solution: use the nextFocus property to point to EditText itself: note the final configuration of 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;
    }

Tags: Android Windows xml

Posted on Tue, 05 May 2020 16:32:08 -0400 by nesargha