Domanda

In my edittext, I set TextWatcher on it. I want it to set the thousandseparator automatically.

getDoubleValue is used to get The value of my EditText without the points(in Germany, the thousand-separator is a '.')

If some Text changed, I get the editable from the EditText, remove the Listener to prevent loops, get the doubleValue, clear the editable and set the value with the thousandseparator. But only the last point is visible. If I use setText() instead, it works, but is poorly slow.

this.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            Log.d("debug", "ontextchanged");
            Editable edit = getText();

            InputField.this.removeTextChangedListener(this);
            String ausg = NumberFormat.getInstance(Locale.GERMAN).format(
                    getDoubleValue());
            edit.clear();
            Log.d("debug", ausg);
            edit.append(ausg);
            Log.d("debug", getText().toString());
            InputField.this.addTextChangedListener(this);

        }

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


        }

        @Override
        public void afterTextChanged(Editable s) {


        }
    });

public double getDoubleValue() {
    Log.d("debug", "getDoubleValue");
    try {
        return Double.parseDouble(getText().toString()
                .replaceAll("\\.", ""));
    } catch (Exception e) {
        Log.e("debug", e.toString());
        return 0;
    }
}

My Logcat output: LogCatOutput as screenshot

enter image description here

È stato utile?

Soluzione

I set the inputtype to DecimalNumber, so the edittext only allows one point. the trick was to remove the inputtype.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top