8

我正在使用此代码从头到尾删除可跨文本的格式。问题是它运行成功,但文本中的最后一个字符仍然是粗体(或斜体/下划线)。

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);
4

6 回答 6

16

如果要从文本中删除所有跨度,请使用:

Spannable str = text.getText();    
Object spansToRemove[] = str.getSpans(startSelection, endSelection, Object.class);
    for(Object span: spansToRemove){
        if(span instanceof CharacterStyle)
            spannable.removeSpan(span);
    }
于 2013-11-07T22:11:09.447 回答
3

有一个非常简单的解决方案:

当您将 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
于 2019-09-23T23:03:39.540 回答
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);
于 2013-11-06T12:14:36.083 回答
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()
  }
}
于 2020-10-09T03:07:04.840 回答
0

以下对我有用(鉴于“文本”是您的 TextView 或 EditText):

String str = text.getText().toString();
text.setText(str);
于 2016-05-28T22:22:31.427 回答
0

很简单,重新启动您的 SpannableString。希望这会节省您的时间。

于 2020-12-10T09:20:20.617 回答