From: Damyan Ivanov Date: Sun, 28 Feb 2021 21:26:26 +0000 (+0200) Subject: format amounts when the input field loses focus X-Git-Tag: v0.17.0~61 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=ac1a592ecbdf1557e2455976aa045de7d6b15b03;p=mobile-ledger.git format amounts when the input field loses focus previously this relied on an update via the model --- diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java index e4afc812..d93752e8 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemHolder.java @@ -56,7 +56,7 @@ import java.util.Objects; class NewTransactionItemHolder extends RecyclerView.ViewHolder implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback { - private final String decimalDot; + private final String decimalDot = "."; private final MobileLedgerProfile mProfile; private final NewTransactionRowBinding b; private final NewTransactionItemsAdapter mAdapter; @@ -90,7 +90,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder mProfile = Data.getProfile(); - View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { + @SuppressLint("DefaultLocale") View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { final int id = v.getId(); if (hasFocus) { boolean wasSyncing = syncingData; @@ -119,6 +119,19 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder syncingData = wasSyncing; } } + else { // lost focus + if (id == R.id.account_row_acc_amounts) { + try { + String input = String.valueOf(b.accountRowAccAmounts.getText()); + input = input.replace(decimalSeparator, decimalDot); + b.accountRowAccAmounts.setText( + String.format("%4.2f", Float.parseFloat(input))); + } + catch (NumberFormatException ex) { + // ignored + } + } + } if (id == R.id.comment) { commentFocusChanged(b.comment, hasFocus); @@ -147,8 +160,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder DecimalFormatSymbols.getInstance(locale) .getMonetaryDecimalSeparator())); - decimalDot = "."; - final TextWatcher tw = new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {