Android Mascara, Act different according to API

0

With 4.0.1 for example my mask puts it that way

Now if I put one with version 6.0.0

it appears to me in the following way

Now my concern is because the peso sign appears when my Android version is lower, my code of the mask is as follows

 public TextWatcher amount(final EditText editText) {
    return new TextWatcher() {
        DecimalFormat dec = new DecimalFormat("0.00");
        @Override
        public void afterTextChanged(Editable arg0) {
        }
        @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)){
                editText.removeTextChangedListener(this);

                String cleanString = s.toString().replaceAll("["+getResources().getString(R.string.MonedaMonto)+",.]", "").replace(" ","");

                double parsed = Double.parseDouble(cleanString.replaceAll("\s","").trim());
                // Obtienes la instancia del formateador
                DecimalFormat decimalFormat  = (DecimalFormat) NumberFormat.getCurrencyInstance();

                // obtener la instancia del formatiador de simbolos
                DecimalFormatSymbols symbols = decimalFormat.getDecimalFormatSymbols();

                // cambias el simbolo por US
                symbols.setCurrencySymbol(getResources().getString(R.string.MonedaMonto)+" ");

                // le asignamos el nuevo formateador de simbolo
                decimalFormat.setDecimalFormatSymbols(symbols);

                // formateamos
                String formatted = decimalFormat.format((parsed/100));

                current = formatted;
                editText.setText(formatted);
                editText.setSelection(formatted.length());

                editText.addTextChangedListener(this);
            }
        }
    };
}

Thanks a lot, because I really do not realize why this is

    
asked by Bruno Sosa Fast Tag 05.02.2018 в 19:02
source

1 answer

0

Fix it when creating the

  

DECIMAL FORMAT

Example

DecimalFormat decimalFormat  = (DecimalFormat)NumberFormat.getCurrencyInstance(Locale.US);

In this way the mask acts in the same way always

I leave the whole method

 public TextWatcher amount(final EditText editText) {
    return new TextWatcher() {
        DecimalFormat dec = new DecimalFormat("0.00");
        @Override
        public void afterTextChanged(Editable arg0) {
        }
        @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) && s.toString().compareTo("")!=0){
                editText.removeTextChangedListener(this);


                String cleanString = s.toString().replaceAll("["+getResources().getString(R.string.MonedaMonto)+",.]", "").replace(" ","");

                double parsed = Double.parseDouble(cleanString.replaceAll("\s","").trim());
                // Obtienes la instancia del formateador

                DecimalFormat decimalFormat  = (DecimalFormat)NumberFormat.getCurrencyInstance(Locale.US);


                // obtener la instancia del formatiador de simbolos
                DecimalFormatSymbols symbols = decimalFormat.getDecimalFormatSymbols();

                // cambias el simbolo por US
                symbols.setCurrencySymbol(getResources().getString(R.string.MonedaMonto)+" ");

                // le asignamos el nuevo formateador de simbolo
                decimalFormat.setDecimalFormatSymbols(symbols);

                // formateamos
                String formatted = decimalFormat.format((parsed/100));

                current = formatted;
                editText.setText(formatted);
                editText.setSelection(formatted.length());

                editText.addTextChangedListener(this);
            }
        }
    };
}
    
answered by 05.02.2018 / 21:31
source