Estou com um problema na minha máscara monetária. Tenho um cadastro de produtos, após salvar o produto com os valores certos quando eu chamo o produto para a tela de cadastro novamente ele retorna com dois dígitos a menos. Por exemplo: Quando eu salvo R$ 200,00 e chamo o produto na tela de cadastro ele retorna R$ 2,00. Alguém pode ajudar ? Obrigado.
`public class MonetaryMask implements TextWatcher {
final EditText campo;
public static double stringMonetarioToDouble(String str) {
double retorno = 0;
try {
boolean hasMask = ((str.indexOf("R$") > -1 || str.indexOf("$") > -1) && (str
.indexOf(".") > -1 || str.indexOf(",") > -1));
if (hasMask) {
str = str.replaceAll("[R$]", "").replaceAll("\\,\\w+", "")
.replaceAll("\\.\\w+", "");
}
retorno = Double.parseDouble(str);
} catch (NumberFormatException e) {
//TRATAR EXCEÇÃO
}
return retorno;
}
public MonetaryMask(EditText campo) {
super();
this.campo = campo;
}
private boolean isUpdating = false;
private NumberFormat nf = NumberFormat.getCurrencyInstance();
@Override
public void onTextChanged(CharSequence s, int start, int before,
int after) {
if (isUpdating) {
isUpdating = false;
return;
}
isUpdating = true;
String str = s.toString();
// Verifica se já existe a máscara no texto.
boolean hasMask = ((str.indexOf("R$") > -1 || str.indexOf("$") > -1)
&& (str.indexOf(".") > -1 || str.indexOf(",") > -1));
// Verificamos se existe máscara
if (hasMask) {
// Retiramos a máscara.
str = str.replaceAll("[R$]", "").replaceAll("[,]", "")
.replaceAll("[.]", "");
}
try {
str = nf.format(Double.parseDouble(str) / 100);
campo.setText(str);
campo.setSelection(campo.getText().length());
} catch (NumberFormatException e) {
s = "";
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// Não utilizado
}
@Override
public void afterTextChanged(Editable s) {
}
}
`
