Android Edittext- Clearing Spans

我是研究僧i 提交于 2019-12-03 10:05:57

Had the same problem. Solved it by removing only the types of spans that I added to the EditText. I guess clearSpans removes more than it should. I did something like this for each type of span I used:

toRemoveSpans = et.getSpans(0, et.getText().length(), ForegroundColorSpan.class);
for (int i = 0; i < toRemoveSpans.length; i++) 
    et.removeSpan(toRemoveSpans[i]);
private void clearSpans(@NonNull final Editable editable) {
    final Object[] spans = editable.getSpans(0, editable.length(), Object.class);
    for (final Object span : spans) {
        if (span instanceof ForegroundColorSpan || span instanceof SpannableTextView.CustomTypefaceSpan) {
            editable.removeSpan(span);
        }
    }
}

Depending on type of spans you have added you may have to include more than just ForegroundColorSpan. The above method is a simple drop in replacement and it easy to specify what spans to remove.

the best answer would be this which "s" is the string that you want to show instead of spans or it can be empty String.

the code is in Kotlin

editText.setText(s)
editText.setSelection(editText.text.length)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!