Java 在读取条形码之前清除编辑文本?

Java 在读取条形码之前清除编辑文本?,java,android,barcode,handheld,Java,Android,Barcode,Handheld,如何在使用激光手持设备读取条形码之前删除编辑文本的内容。问题是,当我在setOnKeyListener中时,它已经被读取了。这就是为什么我不能在代码的这一点上删除edittext的内容 我需要了解如何在每次读取条形码时删除文本,而不必触摸任何按钮 mBinding.barcode.setOnKeyListener((View v, int keyCode, KeyEvent event) -> { if (event.getAction() == KeyEvent.

如何在使用激光手持设备读取条形码之前删除编辑文本的内容。问题是,当我在
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);
    }
}
公共无效把手条码(字符串条码){
布尔hasResult=false;
用于(产品:MPProducts){
如果(!TextUtils.isEmpty(product.getBarcode())和&product.getBarcode().equals(barcode)){
hasResult=true;
if(product.getQuantita_eff()getStorage().getDb().products().update(产品))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(()->{
getView().notifyProductAdded(产品,空);
int missingItems=product.getQuantita_prev()-product.getQuantita_eff();
如果(缺失项>0){
getView().notifyMissingItemsForProduct(产品,missingItems);
}
如果(!getView().isScannerEnable()){
new Handler().postDelayed(()->getView().enableScanner(true),2000);
}
},throwable->getView().enableScanner(true));
}否则{
mNroColliMap.put(product.getBarcode(),mNroColliMap.get(product.getBarcode())+1);
}
}否则{
getView().NotifyProductReadyScanding();
}
打破
}
}
如果(!hasResult){
GetContractor用于条形码验证(条形码);
}
}

解决方案:

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

mBinding.barcode.clear()
这将在每次扫描条形码时清除文本,无需任何触摸

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

更新:

试试这个:

public void handleBarcode(EditText edittext, String barcode) {
那么

最后,
edittext.clear()


试试看。

如何启动条形码扫描仪?你有专门的按钮吗?我希望在每次阅读之前的文本时,都能在不触碰任何东西的情况下删除,键盘甚至不可见。你能显示:
handleBarcode(…)的方法内容吗
方法handleBarcode在Presenter类中,而mBinding在主活动中,因此我无法清除Presenter中的编辑文本。我想您需要在Presenter类中获取
Edittext
对象。Presenter类中的
handleBarcode()
中的参数是什么@Erik你能给我看一下签名吗?编辑好了。我无法将参数传递到handleBarcode中,因为我在两种不同的情况下使用此方法,并且仅在两种情况中的一种情况下需要它
    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;
    });