我正在使用此代码从开始到结束删除可跨越文本的格式。问题是它正在成功运行,但文本中的最后一个字符仍然是粗体(或斜体/下划线)。
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);
答案 0 :(得分:9)
如果您想从文本中删除所有范围,请使用以下命令:
Spannable str = text.getText();
Object spansToRemove[] = str.getSpans(startSelection, endSelection, Object.class);
for(Object span: spansToRemove){
if(span instanceof CharacterStyle)
spannable.removeSpan(span);
}
答案 1 :(得分:0)
我稍微更改了代码。看一看。我已经删除了循环中的if条件。它现在正在运作
fromSelectionSpan = true;
int startSelection = 0;
int endSelection = text.length();
Spannable str = text.getText();
StyleSpan[] ss = str.getSpans(startSelection, endSelection, StyleSpan.class);
for (int i = 0; i < ss.length; i++) {
str.removeSpan(ss[i]);
}
UnderlineSpan[] ulSpan = str.getSpans(startSelection, endSelection, UnderlineSpan.class);
for (int i = 0; i < ulSpan.length; i++) {
str.removeSpan(ulSpan[i]);
}
text.setText(str);
b.setChecked(false);
i.setChecked(false);
u.setChecked(false);
text.setSelection(endSelection);
答案 2 :(得分:0)
有一个非常简单的解决方案:
将Spannable对象设置为TextView时,您使用myTextView.setText(spannable);
会添加分配给spannable的自定义格式。
要一次清除TextView
中的所有跨度,请使用:myTextView.setText(spannable.toString());
示例
Spannable spannable = new SpannableString(myTextView.getText().toString());
spannable.setSpan(new ForegroundColorSpan(Color.RED), 8, 13,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new BackgroundColorSpan(Color.YELLOW), 4, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new BackgroundColorSpan(Color.BLUE), 10, 20, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new UnderlineSpan(), 7, 11, 0);
myTextView.setText(spannable); // add all the spannable format
myTextView.setText(spannable.toString()); // clear all the spannable format
答案 3 :(得分:0)
这是我创建的一种小型实用程序方法,是对TextView
的扩展,并在API 29中进行了测试:
fun TextView.removeAttributedProperties (forText: String? = null) {
forText?.let {
// check if the text we're highlighting is empty to abort
if (forText.isEmpty()) {
return
}
// compute the start and end indices from the text
val startIdx = text.indexOf(forText)
val endIdx = startIdx + forText.length
// if the indices are out of bounds, abort as well
if (startIdx < 0 || endIdx > text.length) {
return
}
(text as? SpannedString)?.let { spannedString ->
val spannable = spannedString.toSpannable()
val spansToRemove = spannable.getSpans(startIdx, endIdx, CharacterStyle::class.java)
for (span in spansToRemove) {
spannable.removeSpan(span)
}
// update the text back
text = spannable
}
} ?: run {
// if we have no text, let's remove all attributed properties
text = text.toString()
}
}
答案 4 :(得分:0)
简单,重新启动您的SpannableString。 希望这可以节省您的时间。
答案 5 :(得分:-1)
以下适用于我(假设“text”是TextView或EditText):
String str = text.getText().toString();
text.setText(str);