安卓使用外接扫码枪扫描一维码之后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;
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值