在Android中使用千位分隔符(,)键入时如何格式化EditText的输入?

mah*_*ani 9 java android number-formatting textwatcher android-edittext

我有一个edittext,它只获得没有十进制数字的数字.

android:inputType="number"
Run Code Online (Sandbox Code Playgroud)

我打算在打字的时候把数千人分开.例如25,000.

我知道我应该使用TextWatcher并且我已经使用过这段代码,但是我无法使用它:

@Override
        public void afterTextChanged(Editable viewss) {
            String s = null;
            try {
                // The comma in the format specifier does the trick
                s = String.format("%,d", Long.parseLong(viewss.toString()));
            } catch (NumberFormatException e) {
            }

        }
Run Code Online (Sandbox Code Playgroud)

你能帮我这么做吗?

Adr*_*uer 14

测试这个例子:

import java.text.DecimalFormat;
import java.text.ParseException;

import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;

public class NumberTextWatcher implements TextWatcher {

    private DecimalFormat df;
    private DecimalFormat dfnd;
    private boolean hasFractionalPart;

    private EditText et;

    public NumberTextWatcher(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));
            }
            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)
    {
        if (s.toString().contains(String.valueOf(df.getDecimalFormatSymbols().getDecimalSeparator())))
        {
            hasFractionalPart = true;
        } else {
            hasFractionalPart = false;
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

要使用它,请将TextChangedListener添加到EditText组件.

editText.addTextChangedListener(new NumberTextWatcher(editText));
Run Code Online (Sandbox Code Playgroud)


小智 1

将其添加到应用程序的 Gradle 中,在 XML xmlns:app="http://schemas.android.com/apk/res-auto" 中编译“com.aldoapps:autoformatedittext:0.9.3”

<com.aldoapps.autoformatedittext.AutoFormatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:isDecimal="true"
    android:maxLength="8"
    android:id="@+id/number"/>
Run Code Online (Sandbox Code Playgroud)

该库自动添加 (,) (.)