本文整理汇总了Java中android.text.Spannable.length方法的典型用法代码示例。如果您正苦于以下问题:Java Spannable.length方法的具体用法?Java Spannable.length怎么用?Java Spannable.length使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.Spannable
的用法示例。
在下文中一共展示了Spannable.length方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: formatColor
import android.text.Spannable; //导入方法依赖的package包/类
private static Editable formatColor(Spannable format, String... args)
{
Editable text = editableFactory.newEditable(format);
for(int i = 0; i < text.length(); )
{
if(text.charAt(i) == '$' && i + 1 < text.length())
{
if(Character.isDigit(text.charAt(i + 1)))
{
int digit = Character.digit(text.charAt(i + 1), 10);
Spannable arg = digit < args.length ? IRCFormatting.parse(args[digit]) : new SpannableString("");
text.replace(i, i + 2, arg);
i += arg.length();
continue;
}
if(text.charAt(i + 1) == '$')
{
text.replace(i, i + 2, "$");
i++;
continue;
}
}
i++;
}
return text;
}
示例2: getText
import android.text.Spannable; //导入方法依赖的package包/类
/**
* @param context
* @param spannable
* @param title
* @return
*/
public static SpannableStringBuilder getText(Activity context, Spannable spannable, String title) {
if (spannable == null || spannable.length() <= 0) {
Log.e(TAG, "getText spannable == null || spannable.length() <= 0 >> return new SpannableStringBuilder();");
return new SpannableStringBuilder();
}
SpannableStringBuilder builder = new SpannableStringBuilder(spannable);
builder.clearSpans();// should clear old spans
URLSpan[] urls = spannable.getSpans(0, spannable.length(), URLSpan.class);
if (urls != null) {
for (URLSpan urlSpan : urls) {
if (urlSpan != null) {
MyURLSpan myURLSpan = new MyURLSpan(context, urlSpan.getURL(), title);
builder.setSpan(myURLSpan, spannable.getSpanStart(urlSpan), spannable
.getSpanEnd(urlSpan), Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
}
}
}
return builder;
}
示例3: intercept
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public Target intercept(Spannable text, int startIndex) {
for(int i=startIndex;i<text.length();i++) {
String pattern = "^(\\[(\\w|[\\u4e00-\\u9fa5]){1,4}\\])";
Pattern r = Pattern.compile(pattern);
Matcher m = r.matcher(text.subSequence(startIndex,text.length()));
if(m.find()){
String match = m.group();
if(LovedEmojicons.emojiconMap.containsKey(match)) {
Target target = new LovedTarget(LovedEmojicons.emojiconMap.get(match),match);
return target;
}
}
}
return null;
}
示例4: setColorsToAllHashTags
import android.text.Spannable; //导入方法依赖的package包/类
private void setColorsToAllHashTags(Spannable text) {
int startIndexOfNextHashSign;
int index = 0;
while (index < text.length() - 1) {
char sign = text.charAt(index);
int nextNotLetterDigitCharIndex = index + 1; // we assume it is next. if if was not changed by findNextValidHashTagChar then index will be incremented by 1
if (sign == '#') {
startIndexOfNextHashSign = index;
nextNotLetterDigitCharIndex = findNextValidHashTagChar(text, startIndexOfNextHashSign);
setColorForHashTagToTheEnd(text, startIndexOfNextHashSign, nextNotLetterDigitCharIndex);
}
index = nextNotLetterDigitCharIndex;
}
}
示例5: applyHighlightToMatchRegions
import android.text.Spannable; //导入方法依赖的package包/类
private boolean applyHighlightToMatchRegions(
Spannable str, List<MatchClassification> classifications) {
boolean hasMatch = false;
for (int i = 0; i < classifications.size(); i++) {
MatchClassification classification = classifications.get(i);
if ((classification.style & MatchClassificationStyle.MATCH)
== MatchClassificationStyle.MATCH) {
int matchStartIndex = classification.offset;
int matchEndIndex;
if (i == classifications.size() - 1) {
matchEndIndex = str.length();
} else {
matchEndIndex = classifications.get(i + 1).offset;
}
matchStartIndex = Math.min(matchStartIndex, str.length());
matchEndIndex = Math.min(matchEndIndex, str.length());
hasMatch = true;
// Bold the part of the URL that matches the user query.
str.setSpan(new StyleSpan(android.graphics.Typeface.BOLD),
matchStartIndex, matchEndIndex, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
return hasMatch;
}
示例6: addEmojis
import android.text.Spannable; //导入方法依赖的package包/类
public static void addEmojis(final Context context, final Spannable text, final int emojiSize) {
final SpanRangeList existingSpanRanges = new SpanRangeList(text);
final PrismojiManager prismojiManager = PrismojiManager.getInstance();
int index = 0;
while (index < text.length()) {
final int existingSpanEnd = existingSpanRanges.spanEnd(index);
if (existingSpanEnd == SPAN_NOT_FOUND) {
final int nextSpanStart = existingSpanRanges.nextSpanStart(index);
final int searchRange = nextSpanStart == SPAN_NOT_FOUND ? text.length() : nextSpanStart;
final Emoji found = prismojiManager.findEmoji(text.subSequence(index, searchRange));
if (found != null) {
text.setSpan(new PrismojiSpan(context, found.getResource(), emojiSize), index, index + found.getLength(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += found.getLength();
} else {
index++;
}
} else {
index += existingSpanEnd - index;
}
}
}
示例7: addEmojis
import android.text.Spannable; //导入方法依赖的package包/类
public static void addEmojis(Context context, Spannable text, int emojiSize, int index, int
length, boolean useSystemDefault) {
if (!useSystemDefault) {
int textLength = text.length();
int textLengthToProcess = (length < 0 || length >= textLength - index) ? textLength :
length + index;
EmojiconSpan[] oldSpans = (EmojiconSpan[]) text.getSpans(0, textLength, EmojiconSpan
.class);
for (Object removeSpan : oldSpans) {
text.removeSpan(removeSpan);
}
int i = index;
while (i < textLengthToProcess) {
int skip = 0;
int icon = 0;
char c = text.charAt(i);
if (isSoftBankEmoji(c)) {
icon = getSoftbankEmojiResource(c);
skip = icon == 0 ? 0 : 1;
}
if (icon == 0) {
int unicode = Character.codePointAt(text, i);
skip = Character.charCount(unicode);
if (unicode > 255) {
icon = getEmojiResource(context, unicode);
}
if (icon == 0 && i + skip < textLengthToProcess) {
Character.codePointAt(text, i + skip);
}
}
if (icon > 0) {
text.setSpan(new EmojiconSpan(context, icon, emojiSize), i, i + skip, 33);
}
i += skip;
}
}
}
示例8: setSpanFromMark
import android.text.Spannable; //导入方法依赖的package包/类
private void setSpanFromMark(String tag, Spannable text, Object mark, Object... spans) {
int where = text.getSpanStart(mark);
text.removeSpan(mark);
int len = text.length();
if (where != len) {
for (Object span : spans) {
if (mSpanCallback != null) {
span = mSpanCallback.onSpanCreated(tag, span);
}
text.setSpan(span, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
示例9: highlightSpans
import android.text.Spannable; //导入方法依赖的package包/类
/** Takes text containing mentions and hashtags and makes them the given colour. */
public static void highlightSpans(Spannable text, int colour) {
// Strip all existing colour spans.
int n = text.length();
ForegroundColorSpan[] oldSpans = text.getSpans(0, n, ForegroundColorSpan.class);
for (int i = oldSpans.length - 1; i >= 0; i--) {
text.removeSpan(oldSpans[i]);
}
// Colour the mentions and hashtags.
String string = text.toString();
int start;
int end = 0;
while (end < n) {
char[] chars = { '#', '@' };
FindCharsResult found = findStart(string, end, chars);
start = found.stringIndex;
if (start < 0) {
break;
}
if (found.charIndex == 0) {
end = findEndOfHashtag(string, start);
} else if (found.charIndex == 1) {
end = findEndOfMention(string, start);
} else {
break;
}
if (end < 0) {
break;
}
text.setSpan(new ForegroundColorSpan(colour), start, end,
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
示例10: closeTags
import android.text.Spannable; //导入方法依赖的package包/类
/**
* "Closes" the specified tags on a Spannable by updating the spans to be
* endpoint-exclusive so that future text appended to the end will not take
* on the same styling. Do not call this method directly.
*/
private static void closeTags(Spannable text, Object[] tags) {
int len = text.length();
for (Object tag : tags) {
if (len > 0) {
text.setSpan(tag, 0, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
else {
text.removeSpan(tag);
}
}
}
示例11: shouldShowPopup
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public boolean shouldShowPopup(Spannable text, int cursorPos) {
return text.length() > 0;
}
示例12: shouldDismissPopup
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public boolean shouldDismissPopup(Spannable text, int cursorPos) {
return text.length() == 0;
}
示例13: convertSpannableToIRCString
import android.text.Spannable; //导入方法依赖的package包/类
public static String convertSpannableToIRCString(Context context, Spannable spannable) {
int n;
int pFg = 99;
int pBg = 99;
boolean pBold = false;
boolean pItalic = false;
boolean pUnderline = false;
StringBuilder ret = new StringBuilder(spannable.length());
for (int i = 0; i < spannable.length(); i = n) {
n = spannable.nextSpanTransition(i, spannable.length(), Object.class);
int fg = 99;
int bg = 99;
boolean bold = false;
boolean italic = false;
boolean underline = false;
for (Object span : spannable.getSpans(i, n, Object.class)) {
int flags = spannable.getSpanFlags(span);
if ((flags & Spannable.SPAN_COMPOSING) != 0)
continue;
if (span instanceof ForegroundColorSpan) {
fg = findNearestIRCColor(context, ((ForegroundColorSpan) span).getForegroundColor());
} else if (span instanceof BackgroundColorSpan) {
bg = findNearestIRCColor(context, ((BackgroundColorSpan) span).getBackgroundColor());
} else if (span instanceof StyleSpan) {
int style = ((StyleSpan) span).getStyle();
if (style == Typeface.BOLD || style == Typeface.BOLD_ITALIC)
bold = true;
if (style == Typeface.ITALIC || style == Typeface.BOLD_ITALIC)
italic = true;
} else if (span instanceof UnderlineSpan) {
underline = true;
}
}
if ((!bold && pBold) || (!italic && pItalic) || (!underline && pUnderline)) {
ret.append((char) 0x0F);
pFg = -1;
pBg = -1;
pBold = false;
pItalic = false;
pUnderline = false;
}
if (bold && !pBold)
ret.append((char) 0x02);
if (italic && !pItalic)
ret.append((char) 0x1D);
if (underline && !pUnderline)
ret.append((char) 0x1F);
if (fg != pFg || bg != pBg) {
ret.append((char) 0x03);
ret.append(fg);
ret.append(',');
ret.append(bg);
}
pFg = fg;
pBg = bg;
pBold = bold;
pItalic = italic;
pUnderline = underline;
ret.append(spannable, i, n);
}
return ret.toString();
}
示例14: action
import android.text.Spannable; //导入方法依赖的package包/类
private boolean action(int what, TextView widget, Spannable buffer) {
Layout layout = widget.getLayout();
int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
int areatop = widget.getScrollY();
int areabot = (widget.getHeight() + areatop) - padding;
int linetop = layout.getLineForVertical(areatop);
int linebot = layout.getLineForVertical(areabot);
int first = layout.getLineStart(linetop);
int last = layout.getLineEnd(linebot);
MyURLSpan[] candidates = (MyURLSpan[]) buffer.getSpans(first, last, MyURLSpan.class);
int a = Selection.getSelectionStart(buffer);
int b = Selection.getSelectionEnd(buffer);
int selStart = Math.min(a, b);
int selEnd = Math.max(a, b);
if (selStart < 0 && buffer.getSpanStart(FROM_BELOW) >= 0) {
selEnd = buffer.length();
selStart = selEnd;
}
if (selStart > last) {
selEnd = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
selStart = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
}
if (selEnd < first) {
selEnd = -1;
selStart = -1;
}
int beststart;
int bestend;
int i;
switch (what) {
case 1:
if (selStart != selEnd) {
MyURLSpan[] link = (MyURLSpan[]) buffer.getSpans(selStart, selEnd, MyURLSpan
.class);
if (link.length == 1) {
link[0].onClick(widget);
break;
}
return false;
}
return false;
case 2:
beststart = -1;
bestend = -1;
for (i = 0; i < candidates.length; i++) {
int end = buffer.getSpanEnd(candidates[i]);
if ((end < selEnd || selStart == selEnd) && end > bestend) {
beststart = buffer.getSpanStart(candidates[i]);
bestend = end;
}
}
if (beststart >= 0) {
Selection.setSelection(buffer, bestend, beststart);
return true;
}
break;
case 3:
beststart = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
bestend = ActivityChooserViewAdapter.MAX_ACTIVITY_COUNT_UNLIMITED;
for (i = 0; i < candidates.length; i++) {
int start = buffer.getSpanStart(candidates[i]);
if ((start > selStart || selStart == selEnd) && start < beststart) {
beststart = start;
bestend = buffer.getSpanEnd(candidates[i]);
}
}
if (bestend < Integer.MAX_VALUE) {
Selection.setSelection(buffer, beststart, bestend);
return true;
}
break;
}
return false;
}