当前位置: 首页>>代码示例>>Java>>正文


Java SpannableStringBuilder.getSpans方法代码示例

本文整理汇总了Java中android.text.SpannableStringBuilder.getSpans方法的典型用法代码示例。如果您正苦于以下问题:Java SpannableStringBuilder.getSpans方法的具体用法?Java SpannableStringBuilder.getSpans怎么用?Java SpannableStringBuilder.getSpans使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.text.SpannableStringBuilder的用法示例。


在下文中一共展示了SpannableStringBuilder.getSpans方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setHtmlWithLinkClickHandler

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public static void setHtmlWithLinkClickHandler(TextView tv, String html,
                                        Action1<String> linkClickHandler) {
    CharSequence sequence = Html.fromHtml(html);
    SpannableStringBuilder strBuilder = new SpannableStringBuilder(sequence);
    URLSpan[] urls = strBuilder.getSpans(0, sequence.length(), URLSpan.class);
    for (URLSpan span : urls) {
        int start = strBuilder.getSpanStart(span);
        int end = strBuilder.getSpanEnd(span);
        int flags = strBuilder.getSpanFlags(span);
        ClickableSpan clickable = new ClickableSpan() {
            public void onClick(View view) {
                linkClickHandler.call(span.getURL());
            }
        };
        strBuilder.setSpan(clickable, start, end, flags);
        strBuilder.removeSpan(span);
    }
    tv.setText(strBuilder);
    tv.setMovementMethod(LinkMovementMethod.getInstance());
}
 
开发者ID:TryGhost,项目名称:Ghost-Android,代码行数:21,代码来源:AppUtils.java

示例2: makeLinksClickable

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public static void makeLinksClickable(TextView v) {
	SpannableStringBuilder ssb = new SpannableStringBuilder(v.getText());
	URLSpan[] spans = ssb.getSpans(0, ssb.length(), URLSpan.class);
	for (URLSpan span : spans) {
		int start = ssb.getSpanStart(span);
		int end = ssb.getSpanEnd(span);
		final String url = span.getURL();
		ssb.removeSpan(span);
		ClickableSpan cSpan = new ClickableSpan() {
			@Override
			public void onClick(View v2) {
				LinkDialogFragment f = LinkDialogFragment.newInstance(url);
				FragmentManager fm = ((AppCompatActivity) v2.getContext())
						.getSupportFragmentManager();
				f.show(fm, f.getUniqueTag());
			}
		};
		ssb.setSpan(cSpan, start, end, 0);
	}
	v.setText(ssb);
	v.setMovementMethod(ArticleMovementMethod.getInstance());
}
 
开发者ID:rafjordao,项目名称:Nird2,代码行数:23,代码来源:UiUtils.java

示例3: OrgSpannable

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public OrgSpannable(String str) {
    SpannableStringBuilder ssb = OrgFormatter.INSTANCE.parse(context, str);

    string = ssb.toString();

    Object[] allSpans = ssb.getSpans(0, ssb.length() - 1, Object.class);

    spans = new OrgSpan[allSpans.length];

    for (int i = 0; i < allSpans.length; i++) {
        spans[i] = new OrgSpan();

        spans[i].start = ssb.getSpanStart(allSpans[i]);
        spans[i].end = ssb.getSpanEnd(allSpans[i]);
        spans[i].className = allSpans[i].getClass().getSimpleName();

        if (allSpans[i] instanceof URLSpan) {
            spans[i].url = ((URLSpan)allSpans[i]).getURL();
        }
    }
}
 
开发者ID:orgzly,项目名称:orgzly-android,代码行数:22,代码来源:OrgFormatterTest.java

示例4: isInFangContext

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public boolean isInFangContext() {
    SpannableStringBuilder builder = attributedString;
    ClickableSpan[] spans = builder.getSpans(0,
            builder.length(), ClickableSpan.class);
    Map<String, String> dict = SingletonData.getInstance().getFangAliasDict();
    if (spans.length > 0) {
        ClickableSpan span = spans[0];
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        String unit = builder.subSequence(start, end)
                .toString();
        String left = dict.get(unit);
        if (left == null) {
            left = unit;
        }
        if (SingletonData.getInstance().getAllFang().contains(left)
                && start > 0
                && builder.toString().substring(start - 1, start).equals("、")
                && builder.charAt(0) != '*') {
            return true;
        }
    }

    return false;
}
 
开发者ID:hh-in-zhuzhou,项目名称:ShangHanLun,代码行数:26,代码来源:LittleTableViewWindow.java

示例5: isInYaoContext

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public boolean isInYaoContext() {
    SpannableStringBuilder builder = attributedString;
    ClickableSpan[] spans = builder.getSpans(0,
            builder.length(), ClickableSpan.class);
    Map<String, String> dict = SingletonData.getInstance().getYaoAliasDict();
    if (spans.length > 0) {
        ClickableSpan span = spans[0];
        int start = builder.getSpanStart(span);
        int end = builder.getSpanEnd(span);
        String unit = builder.subSequence(start, end)
                .toString();
        String left = dict.get(unit);
        if (left == null) {
            left = unit;
        }
        if (SingletonData.getInstance().getAllYao().contains(left)
                && start > 0
                && builder.toString().substring(start - 1, start).equals("、")) {
            return true;
        }
    }

    return false;
}
 
开发者ID:hh-in-zhuzhou,项目名称:ShangHanLun,代码行数:25,代码来源:LittleTextViewWindow.java

示例6: isInOtherSpan

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
protected <T> boolean isInOtherSpan(SpannableStringBuilder builder, @Nullable Class<T> kind, int length, int startPosition, int endPosition) {
    T[] spans = builder.getSpans(0, length, kind);
    List<int[]> spanRange = new ArrayList<>();
    for (T span : spans) {
        int spanStart = builder.getSpanStart(span);
        int spanEnd = builder.getSpanEnd(span);

        spanRange.add(new int[]{spanStart, spanEnd});
    }

    boolean result = false;
    for (int[] range : spanRange) {
        if (startPosition >= range[0] && startPosition <= range[1]) {
            result = true;
            break;
        }
        if (endPosition >= range[0] && endPosition <= range[1]) {
            result = true;
            break;
        }
    }
    return result;
}
 
开发者ID:angcyo,项目名称:RLibrary,代码行数:24,代码来源:RTextView.java

示例7: clearCachedMark

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private void clearCachedMark(SpannableStringBuilder ssb) {
    Cached[] cs = ssb.getSpans(0, ssb.length(), Cached.class);
    if (cs != null && cs.length > 0) {
        for (Cached c : cs) {
            ssb.removeSpan(c);
        }
    }
}
 
开发者ID:nichbar,项目名称:Aequorea,代码行数:9,代码来源:CachedSpannedParser.java

示例8: addSpacesAroundSpansUntilFixed

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private FixingResult addSpacesAroundSpansUntilFixed(SpannableStringBuilder builder,
                                                    int widthMeasureSpec, int heightMeasureSpec) {

    Object[] spans = builder.getSpans(0, builder.length(), Object.class);
    List<Object> spansWithSpacesBefore = new ArrayList<>(spans.length);
    List<Object> spansWithSpacesAfter = new ArrayList<>(spans.length);

    for (Object span : spans) {
        int spanStart = builder.getSpanStart(span);
        if (isNotSpace(builder, spanStart - 1)) {
            builder.insert(spanStart, " ");
            spansWithSpacesBefore.add(span);
        }

        int spanEnd = builder.getSpanEnd(span);
        if (isNotSpace(builder, spanEnd)) {
            builder.insert(spanEnd, " ");
            spansWithSpacesAfter.add(span);
        }

        try {
            setTextAndMeasure(builder, widthMeasureSpec, heightMeasureSpec);
            return FixingResult.fixed(spansWithSpacesBefore, spansWithSpacesAfter);
        } catch (IndexOutOfBoundsException ignored) {
        }
    }
    if (HtmlTextView.DEBUG) {
        Log.d(HtmlTextView.TAG, "Could not fix the Spanned by adding spaces around spans");
    }
    return FixingResult.notFixed();
}
 
开发者ID:RanKKI,项目名称:PSNine,代码行数:32,代码来源:JellyBeanSpanFixTextView.java

示例9: computeCharacterStyleRanges

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private List<Range<Integer>> computeCharacterStyleRanges(CharSequence text) {
    final SpannableStringBuilder ssb = SpannableStringBuilder.valueOf(text);
    final CharacterStyle[] characterStyles = ssb.getSpans(0, ssb.length(), CharacterStyle.class);

    if (characterStyles == null || characterStyles.length == 0) {
        return Collections.EMPTY_LIST;
    }

    List<Range<Integer>> ranges = new ArrayList<>();
    for (CharacterStyle characterStyle : characterStyles) {
        ranges.add(new Range<>(ssb.getSpanStart(characterStyle), ssb.getSpanEnd(characterStyle)));
    }

    return ranges;
}
 
开发者ID:fengdongfei,项目名称:CXJPadProject,代码行数:16,代码来源:EllipsizeTextView.java

示例10: getFontFamilySpan

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
 * Returns the current FontFamilySpan in use on the given subsection of the builder.
 *
 * If no FontFamily has been set yet, spanner.getDefaultFont() is returned.
 *
 * @param builder the text to check
 * @param start start of the section
 * @param end end of the section
 * @return a FontFamily object
 */
private FontFamilySpan getFontFamilySpan( SpannableStringBuilder builder, int start, int end ) {

    FontFamilySpan[] spans = builder.getSpans(start, end, FontFamilySpan.class);

    if ( spans != null && spans.length > 0 ) {
        return spans[spans.length-1];
    }

    return null;
}
 
开发者ID:SysdataSpA,项目名称:SDHtmlTextView,代码行数:21,代码来源:StyleCallback.java

示例11: getTextWithPasswords

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private static String getTextWithPasswords(CharSequence seq) {
    SpannableStringBuilder lstr = new SpannableStringBuilder(seq);
    for (PasswordSpan span : lstr.getSpans(0, lstr.length(), PasswordSpan.class)) {
        lstr.replace(lstr.getSpanStart(span), lstr.getSpanEnd(span), span.mPassword);
        lstr.removeSpan(span);
    }
    return lstr.toString();
}
 
开发者ID:MCMrARM,项目名称:revolution-irc,代码行数:9,代码来源:AutoRunCommandListEditText.java

示例12: isCached

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private boolean isCached(SpannableStringBuilder ssb) {
    Cached[] cs = ssb.getSpans(0, ssb.length(), Cached.class);
    return cs != null && cs.length > 0;
}
 
开发者ID:nichbar,项目名称:Aequorea,代码行数:5,代码来源:CachedSpannedParser.java

示例13: addEmojis

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
   * Convert emoji characters of the given Spannable to the according emojicon.
   *
   * @param context
   * @param text
   * @param emojiSize
   * @param index
   * @param length
   * @param useSystemDefault
   */
  public static SpannableStringBuilder addEmojis(Context context, SpannableStringBuilder text, int emojiSize, int textSize, int index, int length, boolean useSystemDefault) {
      if (useSystemDefault) {
          return text;
      }

      int textLengthToProcess = calculateLegalTextLength(text, index, length);

      // remove spans throughout all text
      EmojiconSpan[] oldSpans = text.getSpans(0, text.length(), EmojiconSpan.class);
      for (int i = 0; i < oldSpans.length; i++) {
          text.removeSpan(oldSpans[i]);
      }

      int[] results = new int[3];
      String textStr = text.toString();

      int processIdx = index;
      while (processIdx < textLengthToProcess) {

          boolean isEmoji = findEmoji(textStr, processIdx, textLengthToProcess, results);
          int skip = results[1];
          if (isEmoji) {
              int icon = results[0];
              boolean isQQFace = results[2] > 0;
          	EmojiconSpan span = new EmojiconSpan(context, icon, (int)(emojiSize * EMOJIICON_SCALE),
                      (int)(emojiSize * EMOJIICON_SCALE));
              span.setTranslateY(isQQFace ? QQFACE_TRANSLATE_Y : EMOJIICON_TRANSLATE_Y);
          	if (span.getCachedDrawable() == null) {
          		text.replace(processIdx, processIdx + skip,  "..");
          		//重新计算字符串的合法长度
          		textLengthToProcess = calculateLegalTextLength(text, index, length);
		} else {
			text.setSpan(span, processIdx, processIdx + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
          }
          
          processIdx += skip;
}
      return (SpannableStringBuilder) text.subSequence(index, processIdx);
  }
 
开发者ID:coopese,项目名称:qmui,代码行数:51,代码来源:EmojiconHandler.java

示例14: setEmoteSpans

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private void setEmoteSpans(SpannableStringBuilder builder) {
    for (URLSpan span : builder.getSpans(0, builder.length(), URLSpan.class)) {
            setLargeLinks(builder, span);
        File emoteDir = new File(Environment.getExternalStorageDirectory(), "RedditEmotes");
        File emoteFile = new File(emoteDir, span.getURL().replace("/", "").replaceAll("-.*", "")
                + ".png"); //BPM uses "-" to add dynamics for emotes in browser. Fall back to original here if exists.
        boolean startsWithSlash = span.getURL().startsWith("/");
        boolean hasOnlyOneSlash = StringUtils.countMatches(span.getURL(), "/") == 1;

        if (emoteDir.exists() && startsWithSlash && hasOnlyOneSlash && emoteFile.exists()) {
            //We've got an emote match
            int start = builder.getSpanStart(span);
            int end = builder.getSpanEnd(span);
            CharSequence textCovers = builder.subSequence(start, end);

            //Make sure bitmap loaded works well with screen density.
            BitmapFactory.Options options = new BitmapFactory.Options();
            DisplayMetrics metrics = new DisplayMetrics();
            ((WindowManager) getContext().getSystemService(
                    Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
            options.inDensity = 240;
            options.inScreenDensity = metrics.densityDpi;
            options.inScaled = true;

            //Since emotes are not directly attached to included text, add extra character to attach image to.
            builder.removeSpan(span);
            if (builder.subSequence(start, end).charAt(0) != '.') {
                builder.insert(start, ".");
            }
            Bitmap emoteBitmap = BitmapFactory.decodeFile(emoteFile.getAbsolutePath(), options);
            builder.setSpan(new ImageSpan(getContext(), emoteBitmap), start, start + 1,
                    Spanned.SPAN_INCLUSIVE_INCLUSIVE);
            //Check if url span has length. If it does, it's a spoiler/caption
            if (textCovers.length() > 1) {
                builder.setSpan(new URLSpan("/sp"), start + 1, end + 1,
                        Spanned.SPAN_INCLUSIVE_INCLUSIVE);
                builder.setSpan(new StyleSpan(Typeface.ITALIC), start + 1, end + 1,
                        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
            builder.append("\n"); //Newline to fix text wrapping issues
        }
    }
}
 
开发者ID:ccrama,项目名称:Slide-RSS,代码行数:44,代码来源:SpoilerRobotoTextView.java

示例15: addEmojis

import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
 * Convert emoji characters of the given Spannable to the according emojicon.
 *
 * @param context
 * @param text
 * @param emojiSize
 * @param index
 * @param length
 * @param useSystemDefault
 */
public static SpannableStringBuilder addEmojis(Context context, SpannableStringBuilder text, int emojiSize, int textSize, int index, int length, boolean useSystemDefault) {
    if (useSystemDefault) {
        return text;
    }

    int textLengthToProcess = calculateLegalTextLength(text, index, length);

    // remove spans throughout all text
    EmojiconSpan[] oldSpans = text.getSpans(0, text.length(), EmojiconSpan.class);
    for (EmojiconSpan oldSpan : oldSpans) {
        text.removeSpan(oldSpan);
    }

    int[] results = new int[3];
    String textStr = text.toString();

    int processIdx = index;
    while (processIdx < textLengthToProcess) {

        boolean isEmoji = findEmoji(textStr, processIdx, textLengthToProcess, results);
        int skip = results[1];
        if (isEmoji) {
            int icon = results[0];
            boolean isQQFace = results[2] > 0;
            EmojiconSpan span = new EmojiconSpan(context, icon, (int) (emojiSize * EMOJIICON_SCALE),
                    (int) (emojiSize * EMOJIICON_SCALE));
            span.setTranslateY(isQQFace ? QQFACE_TRANSLATE_Y : EMOJIICON_TRANSLATE_Y);
            if (span.getCachedDrawable() == null) {
                text.replace(processIdx, processIdx + skip, "..");
                //重新计算字符串的合法长度
                textLengthToProcess = calculateLegalTextLength(text, index, length);
            } else {
                text.setSpan(span, processIdx, processIdx + skip, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        }

        processIdx += skip;
    }
    return (SpannableStringBuilder) text.subSequence(index, processIdx);
}
 
开发者ID:QMUI,项目名称:QMUI_Android,代码行数:51,代码来源:EmojiconHandler.java


注:本文中的android.text.SpannableStringBuilder.getSpans方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。