【发布时间】:2019-07-08 05:34:13
【问题描述】:
我有一个 Edittext 并想设置 EditText 以便当用户输入要转换的数字时,应将千位分隔符 (,) 实时自动添加到数字中。但我想在“onTextChanged”中执行此操作" 方法不在 "afterTextChanged" 方法中。我该怎么办?
public class NumberTextWatcherForThousand implements TextWatcher {
EditText editText;
public NumberTextWatcherForThousand(EditText editText) {
this.editText = editText;
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
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()));
edittext.settext(s);
} catch (NumberFormatException e) {
}
}
【问题讨论】:
标签: java android android-edittext