Edittext退格长保持不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Edittext退格长保持不起作用相关的知识,希望对你有一定的参考价值。
我有Edittext
,其中我添加了TextWatcher
.when我按退格键的工作,但如果我持有退格键,它不会删除所有文本
public sTextWatcher implements TextWatcher{
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence source, int start, int before, int count) {
}
}
@Override
public void afterTextChanged(Editable s) {
edittext.setText(s.toString());
}
答案
试试这个代码
editText.addTextChangedListener( new TextWatcher() {
boolean isEdiging;
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override public void afterTextChanged(Editable s) {
if(isEdiging) return;
isEdiging = true;
String str = s.toString().replaceAll("[^\d]", "");
double s1 = 0;
try {
s1 = Double.parseDouble(str);
}catch (NumberFormatException e){
e.printStackTrace();
}
NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
((DecimalFormat)nf2).applyPattern("###,###.###");
s.replace(0, s.length(), nf2.format(s1));
isEdiging = false;
} editText.addTextChangedListener( new TextWatcher() {
boolean isEdiging;
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override public void afterTextChanged(Editable s) {
if(isEdiging) return;
isEdiging = true;
String str = s.toString().replaceAll("[^\d]", "");
double s1 = 0;
try {
s1 = Double.parseDouble(str);
}catch (NumberFormatException e){
e.printStackTrace();
}
NumberFormat nf2 = NumberFormat.getInstance(Locale.ENGLISH);
((DecimalFormat)nf2).applyPattern("###,###.###");
s.replace(0, s.length(), nf2.format(s1));
isEdiging = false;
}
});
});
以上是关于Edittext退格长保持不起作用的主要内容,如果未能解决你的问题,请参考以下文章