how can I delete the contents of an edit text before reading a barcode with the laser handheld. The problem is that when I am in the setOnKeyListener
, it has already been read. This is why I can not delete the contents of edittext at this point in the code.
I would need to understand how to delete the text every time a barcode is read without touching any buttons.
mBinding.barcode.setOnKeyListener((View v, int keyCode, KeyEvent event) -> {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
presenter.handleBarcode(mBinding.barcode.getText().toString().trim());
}
return true;
});
This is handleBarcode method:
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);
}
}
Solution:
Inside the method handleBarcode(..)
you can write:
mBinding.barcode.clear()
This will clear the text each time you scan the barcode without any touches.
Let's hope this will get you what you want.
UPDATE:
Try this:
public void handleBarcode(EditText edittext, String barcode) {
Then,
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;
});
Finally, edittext.clear()
Try it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.