简体   繁体   English

如何在Android TextView上使用removeSpan()?

[英]How to use removeSpan() on android textview?

Pardon me if I'm asking a dumb question but I would like to know how to remove a span from text in my textview. 如果我问一个愚蠢的问题,请原谅我,但我想知道如何从textview中的文本中删除一个跨度。 This is how my span method looks like. 这就是我的span方法的样子。

public CharSequence setTextStyleItalic(CharSequence text) {
    StyleSpan style = new StyleSpan(Typeface.ITALIC);
    SpannableString str = new SpannableString(text);
    str.setSpan(style, 0, text.length(), 0);
    return str;
}

and this is how i call it. 这就是我所说的。

tvTitle.setText(setTextStyleItalic(tvTitle.getText()));

I would really like to know how to remove this italic span in java using removeSpan() please. 我真的很想知道如何在Java中使用removeSpan()删除此斜体跨度。

Get the text of the TextView and cast it to SpannableString then you can use the getSpans(int queryStart, int queryEnd, Class<T> kind) method to iterate over those spans and remove them 获取TextView的文本并将其转换为SpannableString然后可以使用getSpans(int queryStart, int queryEnd, Class<T> kind)方法遍历这些跨度并将其删除

SpannableString ss=(SpannableString)txtView.getText();
ForegroundColorSpan[] spans=ss.getSpans(0, txtView.getText().length(), ForegroundColorSpan.class);
for(int i=0; i<spans.length; i++){
  ss.removeSpan(spans[i]);
}

First get text from your span using getSpans() 首先使用getSpans()从您的范围中获取文本

  removeTextSpan = tvTitle.getSpans(0, tvTitle.getText().length(),ForegroundColorSpan.class);
  for (int i = 0; i < removeTextSpan.length; i++) 
  tvTitle.removeSpan(removeTextSpan[i]);

i think this will help 我认为这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM