我正在使用此代码从头到尾删除可跨文本的格式。问题是它运行成功,但文本中的最后一个字符仍然是粗体(或斜体/下划线)。
removeSpan
不适用于文本中的最后一个字符:
int startSelection = 0;
int endSelection = text.length();
if(startSelection > endSelection) {
startSelection = text.getSelectionEnd();
endSelection = text.getSelectionStart();
}
Spannable str = text.getText();
StyleSpan[] ss = str.getSpans(startSelection, endSelection, StyleSpan.class);
for (int i = 0; i < ss.length; i++) {
if (ss[i].getStyle() == android.graphics.Typeface.BOLD) {
str.removeSpan(ss[i]);
}
if (ss[i].getStyle() == android.graphics.Typeface.ITALIC) {
str.removeSpan(ss[i]);
}
}
UnderlineSpan[] ulSpan = str.getSpans(startSelection, endSelection, UnderlineSpan.class);
for (int i = 0; i < ulSpan.length; i++) {
str.removeSpan(ulSpan[i]);
}
str.removeSpan(ss[1]);
text.setText(str);