android.text.SpannableString.getSpans()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(10.7k)|赞(0)|评价(0)|浏览(273)

本文整理了Java中android.text.SpannableString.getSpans()方法的一些代码示例,展示了SpannableString.getSpans()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SpannableString.getSpans()方法的具体详情如下:
包路径:android.text.SpannableString
类名称:SpannableString
方法名:getSpans

SpannableString.getSpans介绍

暂无

代码示例

代码示例来源:origin: nickbutcher/plaid

static SpannableStringBuilder linkifyPlainLinks(
    CharSequence input,
    ColorStateList linkTextColor,
    @ColorInt int linkHighlightColor) {
  final SpannableString plainLinks = new SpannableString(input); // copy of input
  // Linkify doesn't seem to work as expected on M+
  // TODO: figure out why
  //Linkify.addLinks(plainLinks, Linkify.WEB_URLS);
  final URLSpan[] urlSpans = plainLinks.getSpans(0, plainLinks.length(), URLSpan.class);
  // add any plain links to the output
  final SpannableStringBuilder ssb = new SpannableStringBuilder(input);
  for (URLSpan urlSpan : urlSpans) {
    ssb.removeSpan(urlSpan);
    ssb.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor),
        plainLinks.getSpanStart(urlSpan),
        plainLinks.getSpanEnd(urlSpan),
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  return ssb;
}

代码示例来源:origin: robolectric/robolectric

private void assertBothSpans(URLSpan s1, UnderlineSpan s2) {
 Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class);
 if (spans[0] instanceof URLSpan) {
  assertThat((URLSpan) spans[0]).isSameAs(s1);
 } else {
  assertThat((UnderlineSpan) spans[0]).isSameAs(s2);
 }
 if (spans[1] instanceof UnderlineSpan) {
  assertThat((UnderlineSpan) spans[1]).isSameAs(s2);
 } else {
  assertThat((URLSpan) spans[1]).isSameAs(s1);
 }
}

代码示例来源:origin: stackoverflow.com

public class RichTextUtils {
  public static <A extends CharacterStyle, B extends CharacterStyle> Spannable replaceAll(Spanned original,
  Class<A> sourceType,
  SpanConverter<A, B> converter) {
    SpannableString result=new SpannableString(original);
    A[] spans=result.getSpans(0, result.length(), sourceType);

    for (A span : spans) {
      int start=result.getSpanStart(span);
      int end=result.getSpanEnd(span);
      int flags=result.getSpanFlags(span);

      result.removeSpan(span);
      result.setSpan(converter.convert(span), start, end, flags);
    }

    return(result);
  }

  public interface SpanConverter<A extends CharacterStyle, B extends CharacterStyle> {
    B convert(A span);
  }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testRemoveSpan() {
 URLSpan s1 = new URLSpan("http://www.foobar.com");
 UnderlineSpan s2 = new UnderlineSpan();
 spanStr.setSpan(s1, 12, 33, 0);
 spanStr.setSpan(s2, 1, 10, 0);
 spanStr.removeSpan(s1);
 Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class);
 assertThat(spans).isNotNull();
 assertThat(spans.length).isEqualTo(1);
 assertThat((UnderlineSpan) spans[0]).isSameAs(s2);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testGetSpans() {
 URLSpan s1 = new URLSpan("http://www.foobar.com");
 UnderlineSpan s2 = new UnderlineSpan();
 spanStr.setSpan(s1, 1, 10, 0);
 spanStr.setSpan(s2, 20, 30, 0);
 Object[] spans = spanStr.getSpans(0, TEST_STRING.length(), Object.class);
 assertThat(spans).isNotNull();
 assertThat(spans.length).isEqualTo(2);
 assertBothSpans(s1, s2);
 spans = spanStr.getSpans(0, TEST_STRING.length(), URLSpan.class);
 assertThat(spans).isNotNull();
 assertThat(spans.length).isEqualTo(1);
 assertThat((URLSpan) spans[0]).isSameAs(s1);
 spans = spanStr.getSpans(11, 35, Object.class);
 assertThat(spans).isNotNull();
 assertThat(spans.length).isEqualTo(1);
 assertThat((UnderlineSpan) spans[0]).isSameAs(s2);
 spans = spanStr.getSpans(21, 35, Object.class);
 assertThat(spans).isNotNull();
 assertThat(spans.length).isEqualTo(1);
 assertThat((UnderlineSpan) spans[0]).isSameAs(s2);
 spans = spanStr.getSpans(5, 15, Object.class);
 assertThat(spans).isNotNull();
 assertThat(spans.length).isEqualTo(1);
 assertThat((URLSpan) spans[0]).isSameAs(s1);
}

代码示例来源:origin: wangdan/AisenWeiBo

MyURLSpan[] urlSpans = value.getSpans(0, value.length(), MyURLSpan.class);
  int findStart = 0;
  int findEnd = 0;
    WebURLEmotionSpan[] webURLEmotionSpans = value.getSpans(0, value.length(), WebURLEmotionSpan.class);
    findStart = 0;
    findEnd = 0;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
  WebURLEmotionSpan[] webURLEmotionSpans = value.getSpans(0, value.length(), WebURLEmotionSpan.class);
  for (WebURLEmotionSpan webURLEmotionSpan : webURLEmotionSpans) {
    int start = value.getSpanStart(webURLEmotionSpan);
    LongClickableLinkMovementMethod.getInstance().removeLongClickCallback();
  BackgroundColorSpan[] backgroundColorSpans = value.getSpans(0, value.length(), BackgroundColorSpan.class);
  for (BackgroundColorSpan backgroundColorSpan : backgroundColorSpans) {
    value.removeSpan(backgroundColorSpan);

代码示例来源:origin: wangdan/AisenWeiBo

SpannableString spannableString = SpannableString.valueOf(content);
Linkify.addLinks(spannableString, Pattern.compile("http://[a-zA-Z0-9+&@#/%?=~_\\-|!:,\\.;]*[a-zA-Z0-9+&@#/%=~_|]"), "http://");
URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
for (URLSpan urlSpan : urlSpans) {
  if (!urlSpan.getURL().startsWith("http://t.cn/"))
  SpannableString spannableString = SpannableString.valueOf(content);
  Linkify.addLinks(spannableString, Pattern.compile("http://[a-zA-Z0-9+&@#/%?=~_\\-|!:,\\.;]*[a-zA-Z0-9+&@#/%=~_|]"), "http://");
  URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
  for (URLSpan urlSpan : urlSpans) {
    if (!urlSpan.getURL().startsWith("http://t.cn/"))

代码示例来源:origin: wangdan/AisenWeiBo

Linkify.addLinks(spannableString, dd, scheme);
URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
Object weiboSpan = null;
for (URLSpan urlSpan : urlSpans) {

代码示例来源:origin: wangdan/AisenWeiBo

SpannableString spannableString = SpannableString.valueOf(content);
Linkify.addLinks(spannableString, Pattern.compile("http://[a-zA-Z0-9+&@#/%?=~_\\-|!:,\\.;]*[a-zA-Z0-9+&@#/%=~_|]"), "http://");
URLSpan[] urlSpans = spannableString.getSpans(0, spannableString.length(), URLSpan.class);
for (URLSpan urlSpan : urlSpans) {
  if (!urlSpan.getURL().startsWith("http://t.cn/"))

代码示例来源:origin: stackoverflow.com

SpannableString spannableString = new SpannableString(str);
Linkify.addLinks(spannableString, Linkify.ALL);
URLSpan[] spans = spannableString.getSpans(0, spannableString.length() , URLSpan.class);

代码示例来源:origin: stackoverflow.com

SpannableString ss = new SpannableString(text);
ss.setSpan(new URLSpan("com://my.app"), 3, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
URLSpan[] toRemoveSpans = ss.getSpans(3, 6, URLSpan.class);
ss.removeSpan(toRemoveSpans[0]);   
ss.setSpan(new MyURLSpan("com://my.app"), 3, 6,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

代码示例来源:origin: MCMrARM/revolution-irc

public static CharSequence copyCharSequence(CharSequence msg) {
  SpannableString str = new SpannableString(msg);
  for (Object o : str.getSpans(0, str.length(), NoCopySpan.class))
    str.removeSpan(o);
  return str;
}

代码示例来源:origin: stackoverflow.com

SpannableString ss = getYourSpannableString();
UnderlineSpan[] uspans = ss.getSpans(0, ss.length(), UnderlineSpan.class);
for (UnderlineSpan us : uspans) {
  ss.removeSpan(us);
}

代码示例来源:origin: stackoverflow.com

public static Spannable createClickableSpans(Spanned original,     ClickSpan.OnClickListener listener) {
  SpannableString result = new SpannableString(original);
  URLSpan[] spans = result.getSpans(0, result.length(), URLSpan.class);

  for (URLSpan span : spans) {
    int start = result.getSpanStart(span);
    int end = result.getSpanEnd(span);
    int flags = result.getSpanFlags(span);

    result.removeSpan(span);
    result.setSpan(new ClickSpan(span.getURL(), listener), start, end, flags);
  }

  return result;
}

代码示例来源:origin: stackoverflow.com

private CharSequence fixSpanColor(CharSequence text) {
  if (text instanceof Spanned) {
    final SpannableString s = new SpannableString(text);
    final ForegroundColorSpan[] spans = s.getSpans(0, s.length(), ForegroundColorSpan.class);
    for (final ForegroundColorSpan oldSpan : spans) {
      final ForegroundColorSpan newSpan = new ForegroundColorSpan(oldSpan.getForegroundColor() | 0xFF000000);
      s.setSpan(newSpan, s.getSpanStart(oldSpan), s.getSpanEnd(oldSpan), s.getSpanFlags(oldSpan));
      s.removeSpan(oldSpan);
    }
    return s;
  } else {
    return text;
  }
}

代码示例来源:origin: YeDaxia/Android-YRichEditor

public static <A extends CharacterStyle, B extends CharacterStyle> Spannable replaceAll(Spanned original,
                                            Class<A> sourceType, SpanConverter<A, B> converter) {
 SpannableString result=new SpannableString(original);
 A[] spans=result.getSpans(0, result.length(), sourceType);
 for (A span : spans) {
  int start=result.getSpanStart(span);
  int end=result.getSpanEnd(span);
  int flags=result.getSpanFlags(span);
  
  result.removeSpan(span);
  result.setSpan(converter.convert(span), start, end, flags);
 }
 return(result);
}

代码示例来源:origin: braintree/android-card-form

@Test
public void setSupportedCardTypes_handlesNull() {
  SupportedCardTypesView supportedCardTypesView = new SupportedCardTypesView(RuntimeEnvironment.application);
  supportedCardTypesView.setSupportedCardTypes((CardType[]) null);
  List<PaddedImageSpan> allSpans = Arrays.asList(new SpannableString(supportedCardTypesView.getText())
      .getSpans(0, supportedCardTypesView.length(), PaddedImageSpan.class));
  assertEquals(0, allSpans.size());
}

代码示例来源:origin: braintree/android-card-form

@Test
public void setSelectedCardTypes_handlesNull() {
  SupportedCardTypesView supportedCardTypesView = new SupportedCardTypesView(RuntimeEnvironment.application);
  supportedCardTypesView.setSupportedCardTypes(CardType.VISA, CardType.MASTERCARD, CardType.DISCOVER,
      CardType.AMEX, CardType.DINERS_CLUB, CardType.JCB, CardType.MAESTRO, CardType.UNIONPAY);
  supportedCardTypesView.setSelected((CardType[]) null);
  List<PaddedImageSpan> allSpans = Arrays.asList(new SpannableString(supportedCardTypesView.getText())
      .getSpans(0, supportedCardTypesView.length(), PaddedImageSpan.class));
  assertTrue(allSpans.get(0).isDisabled());
  assertTrue(allSpans.get(1).isDisabled());
  assertTrue(allSpans.get(2).isDisabled());
  assertTrue(allSpans.get(3).isDisabled());
  assertTrue(allSpans.get(4).isDisabled());
  assertTrue(allSpans.get(5).isDisabled());
  assertTrue(allSpans.get(6).isDisabled());
  assertTrue(allSpans.get(7).isDisabled());
}

代码示例来源:origin: stackoverflow.com

@Override
  public boolean onMenuItemSelected(int featureId, MenuItem item) {
  if (item.getTitle() instanceof SpannableString) {
    SpannableString sp = (SpannableString)item.getTitle();
    Object[] spans = sp.getSpans(0, sp.length(), Object.class);
    if (spans != null && spans.length > 0) {
      // set text without span markups, need for super.onMenuItemSelected(featureId, item);
      item.setTitleCondensed(sp.toString());
      boolean result = super.onMenuItemSelected(featureId, item);
      item.setTitleCondensed(sp);
      return result;
    }
  }

  return super.onMenuItemSelected(featureId, item);
}

代码示例来源:origin: andstatus/andstatus

@Override
  public void run() {
    pager.setCurrentItem(HelpActivity.PAGE_LOGO);
    MyUrlSpan.showText(textView, text, TextMediaType.UNKNOWN, true, false);
    if (SpannableString.class.isAssignableFrom(textView.getClass())) {
      SpannableString spannable = (SpannableString) textView.getText();
      URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
      for (URLSpan span : spans) {
        MyLog.i(this, "Clicking on: " + span.getURL());
        span.onClick(textView);
      }
    }
  }
});

相关文章