Android Keyboard with different @ type @

1

Currently I find that using my MASCARA class for the amounts written, as the phone reacts differently, this is my mask class

public class Mask implements TextWatcher {

private DecimalFormat df;
private DecimalFormat dfnd;
private boolean hasFractionalPart;
private int trailingZeroCount;

private EditText et;

public Mask(EditText et)
{
    df = new DecimalFormat("#,###.##");
    df.setDecimalSeparatorAlwaysShown(true);
    dfnd = new DecimalFormat("#,###");
    this.et = et;
    hasFractionalPart = false;
}

@SuppressWarnings("unused")
private static final String TAG = "NumberTextWatcher";

@Override
public void afterTextChanged(Editable s)
{
    et.removeTextChangedListener(this);

    try {
        int inilen, endlen;
        inilen = et.getText().length();

        String v = s.toString().replace(String.valueOf(df.getDecimalFormatSymbols().getGroupingSeparator()), "");
        Number n = df.parse(v);
        int cp = et.getSelectionStart();
        /*if (hasFractionalPart) {
            et.setText(df.format(n));
        } else {
            et.setText(dfnd.format(n));
        }*/
        if (hasFractionalPart) {
            StringBuilder trailingZeros = new StringBuilder();
            while (trailingZeroCount-- > 0)
                trailingZeros.append('0');
            et.setText(df.format(n) + trailingZeros.toString());
        } else {
            et.setText(dfnd.format(n));
        }

        endlen = et.getText().length();
        int sel = (cp + (endlen - inilen));
        if (sel > 0 && sel <= et.getText().length()) {
            et.setSelection(sel);
        } else {
            // place cursor at the end?
            et.setSelection(et.getText().length() -1 );
        }
    } catch (NumberFormatException nfe) {
        // do nothing?
    } catch (ParseException e) {
        // do nothing?
    }

    et.addTextChangedListener(this);
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
    int index = s.toString().indexOf(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator()));
    trailingZeroCount = 0;
    if (index > -1)
    {
        for (index++; index < s.length(); index++) {
            if (s.charAt(index) == '0')
                trailingZeroCount++;
            else {
                trailingZeroCount = 0;
            }
        }

        hasFractionalPart = true;
    } else {
        hasFractionalPart = false;
    }
    /*if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
    {
        hasFractionalPart = true;
    } else {
        hasFractionalPart = false;
    }*/

}

}

Install my application on 2 phones that I have a J5 2016 and a J7 Prime 2016

here the photos screens

As you can see in one of the photos, if it is correct, mark MILES with comma and decimals with period, but in the other mark thousands with period so I can not put commas for decimals, and it is the same application installed on 2 phones, so I imagine it could be the language or something like that, there is some way through android studio to tell you to use X region or language, thank you already

    
asked by Bruno Sosa Fast Tag 02.11.2017 в 16:02
source

1 answer

1

Fix it with the following code fragment

 Locale localizacion = new Locale("es", "US");
        Locale.setDefault(localizacion);
        Configuration config = new Configuration();
        config.locale = localizacion;
        getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
    
answered by 10.11.2017 / 19:22
source