有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在读取条形码之前是否清除编辑文本?

如何在使用激光手持设备读取条形码之前删除编辑文本的内容。问题是,当我在setOnKeyListener中时,它已经被读取了。这就是为什么我不能在代码的这一点上删除edittext的内容

我需要了解如何在每次读取条形码时删除文本,而不必触摸任何按钮

mBinding.barcode.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                presenter.handleBarcode(mBinding.barcode.getText().toString().trim());
            }
            return true;
        });

这是一种把手代码方法:

 public void handleBarcode(String barcode) {
    boolean hasResult = false;
    for (Product product : mProducts) {
        if (!TextUtils.isEmpty(product.getBarcode()) && product.getBarcode().equals(barcode)) {

            hasResult = true;

            if (product.getQuantita_eff() < product.getQuantita_prev()) {
                /* --------- GESTIONE DEL NUMERO DI COLLI PER ARTICOLO ----------*/
                if (mNroColliMap.containsKey(product.getBarcode())) {
                    mNroColliMap.put(product.getBarcode(), mNroColliMap.get(product.getBarcode()) + 1);
                } else {
                    mNroColliMap.put(product.getBarcode(), 1);
                }
                /* --------- FINE GESTIONE DEL NUMERO DI COLLI PER ARTICOLO ----------*/

                //Controllo se sono stati letti tutti i colli (attualmente basta rileggere lo stesso codice)
                if (mNroColliMap.get(product.getBarcode()) == product.getNro_colli()) {
                    product.setQuantita_eff(product.getQuantita_eff() + 1);
                    product.setDt_lettura_barcode(Utils.formatDateTime(new Date()));
                    product.setStatus(Product.Status.DONE);

                    /* --------- CONTROLLO DATA CONSEGNA TASS E NUMERO COLLI ----------*/
                    if (!product.getData_consegna_tassativa().equals(" ")) {
                        getView().showError("Questo articolo ha data di consegna tassativa il " + product.getData_consegna_tassativa());
                    }
                    /* --------- FINE CONTROLLO DATA CONSEGNA TASS E NUMERO COLLI ----------*/

                    registerDisposable(Completable
                            .fromAction(() -> getStorage().getDb().products().update(product))
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(() -> {
                                getView().notifyProductAdded(product, null);

                                int missingItems = product.getQuantita_prev() - product.getQuantita_eff();
                                if (missingItems > 0) {
                                    getView().notifyMissingItemsForProduct(product, missingItems);
                                }

                                if (!getView().isScannerEnable()) {
                                    new Handler().postDelayed(() -> getView().enableScanner(true), 2000);
                                }
                            }, throwable -> getView().enableScanner(true)));
                } else {
                    mNroColliMap.put(product.getBarcode(), mNroColliMap.get(product.getBarcode()) + 1);
                }
            } else {
                getView().notifyProductAlreadyScanned();
            }
            break;
        }
    }
    if (!hasResult) {
        getContractorForBarcodeVerification(barcode);
    }
}

共 (1) 个答案

  1. # 1 楼答案

    解决方案:

    在方法handleBarcode(..)中,您可以编写:

    mBinding.barcode.clear()
    

    这将在每次扫描条形码时清除文本,无需任何触摸

    希望这能让你得到你想要的

    更新:

    试试这个:

    public void handleBarcode(EditText edittext, String barcode) {
    

    那么

        mBinding.barcode.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                presenter.handleBarcode(mBinding.barcode, mBinding.barcode.getText().toString().trim());
            }
            return true;
        });
    

    最后,edittext.clear()

    试试看