如何自动添加千位分隔符的数量是输入的EditText [英] How to Automatically add thousand separators as number is input in EditText
问题描述
林创建转换器应用,我想设置的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()
的<一个href="http://developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged%28android.text.Editable%29"><$c$c>TextWatcher$c$c>. 格式说明逗号的伎俩。
这不适用于浮点输入工作。并注意不要设置无限循环的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
}
这篇关于如何自动添加千位分隔符的数量是输入的EditText的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!