当值等于EditText
时,我试图清除R$0,00
。
我尝试做的是使用edittext.clear();
清除文字,但没有成功,使用valor.removeTextChangedListener(new Ferramentas.EditValor(valor));
也没有改变一件事,那我该怎么做呢?
public static class EditValor implements TextWatcher {
private String current = "";
private EditText valor;
public EditValor(EditText text){
this.valor = text;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if (!s.toString().equals(current)) {
current = valorMoeda(s.toString());
valor.setText(valorMoeda(s.toString()));
valor.setSelection(valorMoeda(s.toString()).length());
}
}
}
ValorMoeda:
public static String valorMoeda(String s) {
String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
String cleanString = s.replaceAll(replaceable, "");
double parsed;
try {
parsed = Double.parseDouble(cleanString);
} catch (NumberFormatException e) {
parsed = 0.00;
}
String formatted = NumberFormat.getCurrencyInstance().format((parsed / 100));
return formatted;
}
答案 0 :(得分:1)
您可以使用afterTextChanged()
方法清除文字,例如:
yourEditText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
if("R$0,00".equals(s.toString())){
yourEditText.setText(""); // or yourEditText.getText().clear();
}
}
});
<强>更新强>:
试试这段代码:
@Override
public void afterTextChanged(Editable s) {
String newString = s.toString();
if (newString.length>0 && !newString.equals(current)) {
String newFormattedValue = valorMoeda(s.toString());
if("R$0,00".equals(newFormattedValue)){
valor.setText("");
} else {
current = newFormattedValue;
valor.setText(newFormattedValue);
valor.setSelection(newFormattedValue.length());
}
}
}