【问题标题】:Clear EditText before reading barcode?在读取条形码之前清除 EditText?
【发布时间】:2019-04-02 04:04:03
【问题描述】:

如何在使用激光手持设备读取条形码之前删除编辑文本的内容。问题是当我在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;
        });

这是handleBarcode方法:

 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);
    }
}

【问题讨论】:

  • 如何启动条码扫描器?你有专门的按钮吗?

标签: java android barcode handheld


【解决方案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()

试试看。

【讨论】:

  • 我希望每次阅读之前的文本都被删除而不接触任何东西,键盘甚至不可见
  • 能否展示一下handleBarcode(..)的方法内容
  • handleBarcode 方法在 Presenter 类中,而 mBinding 在主要活动中,所以我猜我无法清除 Presenter 中的编辑文本
  • 你需要在Presenter类中获取Edittext对象。 Presenter 类中handleBarcode() 的参数是什么? @Erik 你能出示它的签名吗?
  • 已编辑。我无法将参数传递给 handleBarcode,因为我在两种不同的情况下使用此方法,并且仅在两种情况中的一种情况下我需要它
猜你喜欢
  • 2013-08-18
  • 2020-02-07
  • 2012-01-27
  • 1970-01-01
  • 2012-08-03
  • 2016-10-15
  • 2012-02-13
  • 2020-01-27
  • 2011-06-16
相关资源
最近更新 更多