我正在尝试使用所有的span条件(如果没有)进行文本样式设置,但是由于spanStart在0等之前开始时,我总是会遇到错误。任何人都可以为我提供哪些条件,我应该检查哪些条件才能为我的编辑文本赋予样式。这是我的代码
Spannable str = texto.getText();
int selStart = texto.getSelectionStart();
int selEnd = texto.getSelectionEnd();
StyleSpan[] styleSpans = texto.getText().getSpans(selStart, selEnd, StyleSpan.class);
for (StyleSpan styleSpan : styleSpans) {
int oldStyle = styleSpan.getStyle();
int spanStart = str.getSpanStart(styleSpan);
int spanEnd = str.getSpanEnd(styleSpan);
if (spanStart < texto.getSelectionStart()) {
str.setSpan(new StyleSpan(oldStyle), spanStart, selStart, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (spanEnd > texto.getSelectionEnd()) {
str.setSpan(new StyleSpan(oldStyle), selEnd, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (selEnd > selStart) {
if (oldStyle == Typeface.ITALIC) {
str.removeSpan(styleSpan);
italicExists = true;
}
if (oldStyle == Typeface.BOLD_ITALIC) {
str.setSpan(new StyleSpan(Typeface.BOLD), spanStart, spanEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
italicExists = false;
}
}