如何自动添加千位分隔符的数量是输入的EditText数量、分隔符、EditText

由网友(香蕉炖黑木耳)分享简介:林创建转换器应用,我想设置的EditText使得当用户输入要转换的数字,一个千符(,)应automaticaaly实时加到数一旦increaments由3位数字。 ....千,万,十亿等并且当擦除到4以下附图的数目返回到正常。任何帮助吗?谢谢你。Im creating a convertor applicati...

林创建转换器应用,我想设置的EditText使得当用户输入要转换的数字,一个千符(,)应automaticaaly实时加到数一旦increaments由3位数字。 ....千,万,十亿等 并且当擦除到4以下附图的数目返回到正常。 任何帮助吗? 谢谢你。

Im creating a convertor application, I want to set the EditText so that when the user is inputting the number to be converted, a thousand separator (,) should be added automaticaaly in realtime to the number once it increaments by 3 figures.....thousand, million, billion etc and when erased to below 4 figures the number goes back to normal. Any help? Thank You.

推荐答案

您可以使用的String.Format()的TextWatcher. 格式说明逗号的伎俩。

这不适用于浮点输入工作。并注意不要设置无限循环的TextWatcher。

This does not work for floating point input. And be careful not to set an infinite loop with the TextWatcher.

public void afterTextChanged(Editable view) {
    String s = null;
    try {
        // The comma in the format specifier does the trick
        s = String.format("%,d", Long.parseLong(view.toString()));
    } catch (NumberFormatException e) {
    }
    // Set s back to the view after temporarily removing the text change listener
}
阅读全文

相关推荐

最新文章