我有一个EditText实现TextWatcher但Backspace键有问题。 保持退格时前5个字符逐个清晰,但保留退格时,转换为图像的最后5个字符无法逐个清除。
查看图片:
MainActivity.java
public class MainActivity extends AppCompatActivity {
Spannable.Factory spannableFactory;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spannableFactory = Spannable.Factory
.getInstance();
final EditText editText = (EditText) findViewById(R.id.editText);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
editText.removeTextChangedListener(this);
if (s.length() >= 5) {
editText.setText(getTextToIcon(getApplicationContext(), s));
}
if(count!=0){ // != key delete
if (s.length() - 1 > start) {
editText.setSelection(start + 1);
} else {
editText.setSelection(s.length());
}
}else{ // = key delete
editText.setSelection(start);
}
editText.addTextChangedListener(this);
}
@Override
public void afterTextChanged(Editable s) {}
});
}
public Spannable getTextToIcon(Context context, CharSequence text) {
StringBuilder stringBuilder = new StringBuilder(text);
Spannable spannable = spannableFactory.newSpannable(stringBuilder);
int index = text.length() - 1;
for(int i = 5; i<=index; i++){
spannable.setSpan(new ImageSpan(context, android.R.drawable.star_on),
i, i + 1,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return spannable;
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLength="10"
android:inputType="number" />
</LinearLayout>
答案 0 :(得分:4)
兄弟,有bug的代码如下:
if (s.length() >= 5) {
editText.setText(getTextToIcon(getApplicationContext(), s));
}
您应该将其更改为:
if (count != 0 && s.length() >= 5) {
// just set the text to the icon in input state instead of pressing delete key in soft method
editText.setText(getTextToIcon(getApplicationContext(), s));
}
希望有所帮助。
答案 1 :(得分:1)
现在我从这个链接找到了一个解决方案 https://stackoverflow.com/a/10954719/5887320 希望它能帮助其他人。
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;
}
});
}