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

标签 java android android-edittext number-formatting textwatcher

我有一个 edittext ,它只得到没有小数的数字。

android:inputType="number"

我想在打字时将几千个分开。例如 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) {
            }

        }

你能帮我这样做吗?

最佳答案

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

<com.aldoapps.autoformatedittext.AutoFormatEditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:isDecimal="true"
    android:maxLength="8"
    android:id="@+id/number"/>

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

关于java - 在Android中使用千位分隔符(,)输入时如何格式化EditText的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28757931/

相关文章:

java - 如何保护字符串免受 XSS 攻击?

android - 无法从最终 Snackbar 继承

java - 导航说明

java - 我正在使用一个编辑文本,它已经是空白的,因为我想认识一些听众

java - 在 EditText 中输入密码时启动 Activity

android - 禁用在editText android中输入

java - 从 .jar 文件中提取源代码

java - 编写带有外部节点的xml文件

java - DriverManager.getConnection 返回 null

java - Android 如何在 android iframe webview 中传递 url?