From ac1a592ecbdf1557e2455976aa045de7d6b15b03 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 28 Feb 2021 23:26:26 +0200 Subject: [PATCH] format amounts when the input field loses focus previously this relied on an update via the model --- .../NewTransactionItemHolder.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) 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) { -- 2.39.5