0
Modify the onChanged of Textedit via code that works. And you don’t even need a mask for it. Follow an example:
seu_text_edit.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
private String current = "";
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(!s.toString().equals(current)) {
Locale myLocale = new Locale("pt", "BR");
//Nesse bloco ele monta a maskara para money
txtUnitario.removeTextChangedListener(this);
String cleanString = s.toString().replaceAll("[R$,.]", "");
Decimal parsed = Double.parseDouble(cleanString);
String formatted = NumberFormat.getCurrencyInstance(myLocale).format((parsed / 100));
current = formatted;
txtUnitario.setText(formatted);
txtUnitario.setSelection(formatted.length());
//Nesse bloco ele faz a conta do total (Caso a qtde esteja preenchida)
String qtde = txtQtdeLitros.getText().toString();
txtUnitario.addTextChangedListener(this);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
This is just a basic way to make a monetary field, there are many other ways to do and in my opinion, this type of field does not need a mask because you just say that it is currency type and treat the data using Decimal ( Always avoid working with Double or Float values when monetary ).
Detail, Using this logic instead of a mask vc will also give a super cool effect to Edittext as it will fill in from the pennies.
It’s just that I need to perform calculations, I can still use your code?
– fabricio b.
You can, no problem. In my system I make a calculation of liters x value, I use this same code for unitario value, liters and total and works 100%
– Matheus Suffi
Hello, I had a problem how can I save the generated value in the MYSQL database? I will save as String it saved in this format R$ 2.99 or I can save as Decimal? but it shows the following error after saving in String. I’ll post it in the edit.
– fabricio b.