本文整理汇总了Java中android.text.Spannable类的典型用法代码示例。如果您正苦于以下问题:Java Spannable类的具体用法?Java Spannable怎么用?Java Spannable使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Spannable类属于android.text包,在下文中一共展示了Spannable类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: applyLetterSpacing
import android.text.Spannable; //导入依赖的package包/类
private SpannableString applyLetterSpacing(String text) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < text.length(); i++) {
builder.append(text.charAt(i));
if (i + 1 < text.length() && !isEmojiCharacter(text.charAt(i))) {//avoid adding the blank code between the emoji code
builder.append("\u00A0");
}
}
SpannableString finalText = new SpannableString(builder.toString());
if (builder.toString().length() > 1) {
for (int i = 1; i < builder.toString().length(); i += 2) {
if (builder.toString().toCharArray()[i] == 160) {//avoid breaking emoji code, u00A0 is text space
finalText.setSpan(new ScaleXSpan((mLetterSpace + 1) / 10), i, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
return finalText;
}
示例2: bind
import android.text.Spannable; //导入依赖的package包/类
@SuppressWarnings("unused")
public void bind(int position, TweetPublishModel model, RequestManager loader) {
itemView.setTag(model);
Context context = itemView.getContext();
Spannable spannable = TweetParser.getInstance().parse(context, model.getContent());
mTitle.setText(spannable);
mTitle.setMovementMethod(LinkMovementMethod.getInstance());
mTitle.setFocusable(false);
mTitle.setDispatchToParent(true);
mTitle.setLongClickable(false);
mLog.setText(String.format("Error:%s.",
model.getErrorString() == null ? "null" : model.getErrorString()));
mDate.setText(FORMAT.format(new Date(model.getDate())));
}
示例3: findStartPosition
import android.text.Spannable; //导入依赖的package包/类
/**
* 检查当前位置是否命中在spannable上, 如果是, 返回spannable的start position
*/
private int findStartPosition(Spannable spannable, int startWidthPosition) {
CharacterStyle[] oldSpans = spannable.getSpans(startWidthPosition, spannable.length(), CharacterStyle.class);
int position = startWidthPosition;
for (CharacterStyle oldSpan : oldSpans) {
int spanStart = spannable.getSpanStart(oldSpan);
int spanEnd = spannable.getSpanEnd(oldSpan);
if (spanStart <= startWidthPosition && spanEnd > startWidthPosition) {
position = spanStart;
}
if (spanStart >= startWidthPosition) {
spannable.removeSpan(oldSpan);
}
}
//L.e("call: findStartPosition([spannable, startWidthPosition]) " + startWidthPosition + " -> " + position);
return position;
}
示例4: getPressedSpan
import android.text.Spannable; //导入依赖的package包/类
public ITouchableSpan getPressedSpan(TextView textView, Spannable spannable, MotionEvent event) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= textView.getTotalPaddingLeft();
y -= textView.getTotalPaddingTop();
x += textView.getScrollX();
y += textView.getScrollY();
Layout layout = textView.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
if (x < layout.getLineLeft(line) || x > layout.getLineRight(line)) {
// 实际上没点到任何内容
off = -1;
}
ITouchableSpan[] link = spannable.getSpans(off, off, ITouchableSpan.class);
ITouchableSpan touchedSpan = null;
if (link.length > 0) {
touchedSpan = link[0];
}
return touchedSpan;
}
示例5: 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;
}
示例6: convert
import android.text.Spannable; //导入依赖的package包/类
@Override
public void convert(EasyLVHolder holder, int position, BookMark item) {
TextView tv = holder.getView(R.id.tvMarkItem);
SpannableString spanText = new SpannableString((position + 1) + ". " + item.title + ": ");
spanText.setSpan(new ForegroundColorSpan(ContextCompat.getColor(mContext, R.color.light_coffee)),
0, spanText.length(), Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
tv.setText(spanText);
if (item.desc != null) {
tv.append(item.desc
.replaceAll(" ", "")
.replaceAll(" ", "")
.replaceAll("\n", ""));
}
}
示例7: formatHash
import android.text.Spannable; //导入依赖的package包/类
public static Editable formatHash(@Nullable final String prefix, final String address, final int groupSize,
final int lineSize, final char groupSeparator) {
final SpannableStringBuilder builder = prefix != null ? new SpannableStringBuilder(prefix)
: new SpannableStringBuilder();
final int len = address.length();
for (int i = 0; i < len; i += groupSize) {
final int end = i + groupSize;
final String part = address.substring(i, end < len ? end : len);
builder.append(part);
builder.setSpan(new TypefaceSpan("monospace"), builder.length() - part.length(), builder.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (end < len) {
final boolean endOfLine = lineSize > 0 && end % lineSize == 0;
builder.append(endOfLine ? '\n' : groupSeparator);
}
}
return builder;
}
示例8: onTouchEvent
import android.text.Spannable; //导入依赖的package包/类
public boolean onTouchEvent(TextView widget, Spannable buffer, MotionEvent event) {
if (event.getAction() == 1) {
this.mLinkClicked = false;
this.mClickedLink = null;
int x = (((int) event.getX()) - widget.getTotalPaddingLeft()) + widget.getScrollX();
int y = (((int) event.getY()) - widget.getTotalPaddingTop()) + widget.getScrollY();
Layout layout = widget.getLayout();
int off = layout.getOffsetForHorizontal(layout.getLineForVertical(y), (float) x);
ClickableSpan[] link = (ClickableSpan[]) buffer.getSpans(off, off, ClickableSpan
.class);
if (link.length != 0) {
SensibleUrlSpan span = link[0];
this.mLinkClicked = span.onClickSpan(widget);
this.mClickedLink = span.getURL();
return this.mLinkClicked;
}
}
super.onTouchEvent(widget, buffer, event);
return false;
}
示例9: replaceQuoteSpans
import android.text.Spannable; //导入依赖的package包/类
/**
* Replaces the blue line produced by <blockquote>s with something more visible
*
* @param spannable parsed comment text #fromHtml
*/
private void replaceQuoteSpans(Spannable spannable) {
QuoteSpan[] quoteSpans = spannable.getSpans(0, spannable.length(), QuoteSpan.class);
for (QuoteSpan quoteSpan : quoteSpans) {
final int start = spannable.getSpanStart(quoteSpan);
final int end = spannable.getSpanEnd(quoteSpan);
final int flags = spannable.getSpanFlags(quoteSpan);
spannable.removeSpan(quoteSpan);
//If the theme is Light or Sepia, use a darker blue; otherwise, use a lighter blue
final int barColor =
(SettingValues.currentTheme == 1 || SettingValues.currentTheme == 5)
? ContextCompat.getColor(getContext(), R.color.md_blue_600)
: ContextCompat.getColor(getContext(), R.color.md_blue_400);
final int BAR_WIDTH = 4;
final int GAP = 5;
spannable.setSpan(new CustomQuoteSpan(Color.TRANSPARENT, //background color
barColor, //bar color
BAR_WIDTH, //bar width
GAP), //bar + text gap
start, end, flags);
}
}
示例10: replaceWithImages
import android.text.Spannable; //导入依赖的package包/类
/**
* Replace the system emoticons with the provided custom emoticons drawable. This will find the
* unicodes with supported emoticons in the provided text and will replace the emoticons with
* appropriate images.
*
* @param context instance of caller.
* @param text Text to replace
* @param emoticonProvider {@link EmoticonProvider} for emoticons images
* @param emoticonSize Size of the emoticons in dp
* @return Modified text.
*/
public static void replaceWithImages(@NonNull final Context context,
@NonNull final Spannable text,
@NonNull final EmoticonProvider emoticonProvider,
final int emoticonSize) {
final EmoticonSpan[] existingSpans = text.getSpans(0, text.length(), EmoticonSpan.class);
final ArrayList<Integer> existingSpanPositions = new ArrayList<>(existingSpans.length);
for (EmoticonSpan existingSpan : existingSpans) {
existingSpanPositions.add(text.getSpanStart(existingSpan));
}
//Get location and unicode of all emoticons.
final List<EmoticonRange> findAllEmojis = findAllEmoticons(context, text, emoticonProvider);
//Replace all the emoticons with their relatives.
for (int i = 0; i < findAllEmojis.size(); i++) {
final EmoticonRange location = findAllEmojis.get(i);
if (!existingSpanPositions.contains(location.mStartPos)) {
text.setSpan(new EmoticonSpan(context, location.mEmoticon.getIcon(), emoticonSize),
location.mStartPos,
location.mEndPos,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
示例11: getformatedSearchSnippet
import android.text.Spannable; //导入依赖的package包/类
public CharSequence getformatedSearchSnippet() {
//TODO implement improved snippet function
String CleanedSearchString = " " + ArabicUtilities.cleanTextForSearchingWthStingBuilder(searchString) + " ";
StringBuilder cleanedUnformattedPage = new StringBuilder(ArabicUtilities.cleanTextForSearchingWthStingBuilder(unformatedPage));
int firstMatchStart = cleanedUnformattedPage.indexOf(CleanedSearchString);
cleanedUnformattedPage.delete(0, Math.max(firstMatchStart - 100, 0));
cleanedUnformattedPage.delete(
Math.min(firstMatchStart + CleanedSearchString.length() + 100, cleanedUnformattedPage.length())
, cleanedUnformattedPage.length());
cleanedUnformattedPage.insert(0, "...");
cleanedUnformattedPage.append("...");
Spannable snippet = SpannableString.
valueOf(cleanedUnformattedPage.toString());
int index = TextUtils.indexOf(snippet, CleanedSearchString);
while (index >= 0) {
snippet.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
+ CleanedSearchString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
index = TextUtils.indexOf(snippet, CleanedSearchString, index + CleanedSearchString.length());
}
return snippet;
}
示例12: displayPlaylistInfo
import android.text.Spannable; //导入依赖的package包/类
private void displayPlaylistInfo(final Playlist playlist)
{
final TextView textView = new TextView(this);
textView.setPadding(5, 5, 5, 5);
final Spannable message = new SpannableString("Owner: " + playlist.getOwner() + "\nComments: " +
((playlist.getComment() == null) ? "" : playlist.getComment()) +
"\nSong Count: " + playlist.getSongCount() +
((playlist.getPublic() == null) ? "" : ("\nPublic: " + playlist.getPublic()) + ((playlist.getCreated() == null) ? "" : ("\nCreation Date: " + playlist.getCreated().replace('T', ' ')))));
Linkify.addLinks(message, Linkify.WEB_URLS);
textView.setText(message);
textView.setMovementMethod(LinkMovementMethod.getInstance());
new AlertDialog.Builder(this).setTitle(playlist.getName()).setCancelable(true).setIcon(android.R.drawable.ic_dialog_info).setView(textView).show();
}
示例13: createSpanForMatchesP
import android.text.Spannable; //导入依赖的package包/类
/**
* Create Span for matches in paragraph's. Note that this will highlight the full matched pattern
* (including optionals) if no group parameters are given.
*
* @param editable Text editable
* @param pattern The pattern to match
* @param creator A ParcelableSpanCreator for ParcelableSpan
* @param groupsToMatch (optional) groups to be matched, indexes start at 1.
*/
protected void createSpanForMatchesP(final Editable editable, final Pattern pattern, final ParagraphStyleCreator creator, int... groupsToMatch) {
if (groupsToMatch == null || groupsToMatch.length < 1) {
groupsToMatch = new int[]{0};
}
int i = 0;
for (Matcher m = pattern.matcher(editable); m.find(); i++) {
ParagraphStyle span = creator.create(m, i);
if (span != null) {
for (int g : groupsToMatch) {
if (g == 0 || g <= m.groupCount()) {
editable.setSpan(span, m.start(g), m.end(g), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
}
示例14: setAndMergeSpans
import android.text.Spannable; //导入依赖的package包/类
public static void setAndMergeSpans(Spannable text, Object what, int start, int end, int flags) {
Object[] spans = text.getSpans(Math.max(start - 1, 0), Math.min(end + 1, 0), what.getClass());
for (Object span : spans) {
if (!areSpansEqual(span, what))
continue;
int sFlags = text.getSpanFlags(span);
if ((sFlags & Spanned.SPAN_COMPOSING) != 0)
continue;
int sStart = text.getSpanStart(span);
int sEnd = text.getSpanEnd(span);
if (sEnd < start || sStart > end)
continue;
text.removeSpan(span);
if (sStart < start)
start = sStart;
if (sEnd > end)
end = sEnd;
}
text.setSpan(what, start, end, flags);
}
示例15: gatherPhoneLinks
import android.text.Spannable; //导入依赖的package包/类
private static void gatherPhoneLinks(ArrayList<LinkSpec> links,
Spannable s, Pattern pattern, Pattern[] excepts, String[] schemes,
MatchFilter matchFilter, TransformFilter transformFilter) {
Matcher m = pattern.matcher(s);
while (m.find()) {
if (isInExcepts(m.group(), excepts)) {
continue;
}
int start = m.start();
int end = m.end();
if (matchFilter == null || matchFilter.acceptMatch(s, start, end)) {
LinkSpec spec = new LinkSpec();
String url = makeUrl(m.group(0), schemes, m, transformFilter);
spec.url = url;
spec.start = start;
spec.end = end;
links.add(spec);
}
}
}