简体   繁体   中英

Remove value of EditText with TextWatcher when the currency is equals to 0

I'm trying to clean the EditText when the value is equals to R$0,00 .

What I've tried to do is clear the the text using edittext.clear(); but without success, and using valor.removeTextChangedListener(new Ferramentas.EditValor(valor)); also didn't change a thing, so how can I do that?

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;
    }

You can clear your text in afterTextChanged() method, eg:

 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();
          }

        }

});

UPDATE :

Try this code:

    @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());
            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM