[英]Clear EditText before reading barcode?
如何在使用激光手持设备读取条形码之前删除编辑文本的内容。 问题是当我在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);
}
}
解决方案:
在方法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()
尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.