本文整理汇总了Java中android.text.SpannableStringBuilder.replace方法的典型用法代码示例。如果您正苦于以下问题:Java SpannableStringBuilder.replace方法的具体用法?Java SpannableStringBuilder.replace怎么用?Java SpannableStringBuilder.replace使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.SpannableStringBuilder
的用法示例。
在下文中一共展示了SpannableStringBuilder.replace方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: generateStaticLayout
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public static StaticLayout generateStaticLayout(CharSequence text, TextPaint paint, int maxWidth, int smallWidth, int linesCount, int maxLines) {
SpannableStringBuilder stringBuilder = new SpannableStringBuilder(text);
int addedChars = 0;
StaticLayout layout = new StaticLayout(text, paint, smallWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, false);
for (int a = 0; a < linesCount; a++) {
Layout.Directions directions = layout.getLineDirections(a);
if (layout.getLineLeft(a) != 0 || layout.isRtlCharAt(layout.getLineStart(a)) || layout.isRtlCharAt(layout.getLineEnd(a))) {
maxWidth = smallWidth;
}
int pos = layout.getLineEnd(a);
if (pos == text.length()) {
break;
}
pos--;
if (stringBuilder.charAt(pos + addedChars) == ' ') {
stringBuilder.replace(pos + addedChars, pos + addedChars + 1, "\n");
} else if (stringBuilder.charAt(pos + addedChars) != '\n') {
stringBuilder.insert(pos + addedChars, "\n");
addedChars++;
}
if (a == layout.getLineCount() - 1 || a == maxLines - 1) {
break;
}
}
return StaticLayoutEx.createStaticLayout(stringBuilder, paint, maxWidth, Layout.Alignment.ALIGN_NORMAL, 1.0f, dp(1), false, TextUtils.TruncateAt.END, maxWidth, maxLines);
}
示例2: parseOnlyGit
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* 格式化git项目标签
*/
static Spannable parseOnlyGit(final Context context, CharSequence content) {
SpannableStringBuilder builder = new SpannableStringBuilder(content);
Matcher matcher;
while (true) {
matcher = PatternGit.matcher(builder.toString());
if (matcher.find()) {
final String group0 = matcher.group(2);
final String group1 = matcher.group(3);
builder.replace(matcher.start(), matcher.end(), group1);
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(View widget) {
Project project = new Project();
project.setId(Integer.parseInt(group0));
ProjectDetailActivity.show(context, project);
}
};
builder.setSpan(span, matcher.start(), matcher.start() + group1.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
continue;
}
break;
}
return builder;
}
示例3: format
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public static CharSequence format(CharSequence seq, Object... args) {
int argI = 0;
SpannableStringBuilder builder = new SpannableStringBuilder(seq);
for (int i = 0; i < builder.length() - 1; i++) {
if (builder.charAt(i) == '%') {
int c = builder.charAt(++i);
CharSequence replacement = null;
switch (c) {
case 's':
replacement = (CharSequence) args[argI++];
break;
case '%':
replacement = "%";
break;
}
if (replacement != null) {
builder.replace(i - 1, i + 1, replacement);
i += replacement.length() - 2;
}
}
}
return builder;
}
示例4: spannerbleTitleForHeaderInSection
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public SpannableStringBuilder spannerbleTitleForHeaderInSection(
ATableView tableView, int section) {
HH2SectionData sd = data.get(section);
String text = sd.getHeader();
SpannableStringBuilder builder = new SpannableStringBuilder(text);
builder.setSpan(new ForegroundColorSpan(Color.BLUE), 0,
text.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
if (text.contains("$")) {
int index = text.indexOf("$") + 1;
builder.setSpan(new ForegroundColorSpan(Color.RED), index,
text.length(),
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.setSpan(new RelativeSizeSpan(0.55f), index,
text.length(),
SpannableStringBuilder.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.replace(index - 1, index, "\n");
}
return builder;
}
示例5: replaceWithText
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public void replaceWithText(int start, int len, CharSequence text) {
try {
SpannableStringBuilder builder = new SpannableStringBuilder(messageEditText.getText());
builder.replace(start, start + len, text);
messageEditText.setText(builder);
messageEditText.setSelection(start + text.length());
} catch (Exception e) {
FileLog.e("tmessages", e);
}
}
示例6: getUrlTextSpannableString
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private static SpannableStringBuilder getUrlTextSpannableString(Context context, String source,
int size) {
SpannableStringBuilder builder = new SpannableStringBuilder(source);
String prefix = " ";
builder.replace(0, prefix.length(), prefix);
Drawable drawable = context.getResources().getDrawable(R.mipmap.ic_status_link);
drawable.setBounds(0, 0, size, size);
builder.setSpan(new VerticalImageSpan(drawable), prefix.length(), source.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(App.getInstance().getString(R.string.text_url_link));
return builder;
}
示例7: clearHtmlTag
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* 清空HTML标签
*/
public Spannable clearHtmlTag(CharSequence content) {
SpannableStringBuilder builder = new SpannableStringBuilder(content);
Matcher matcher;
while (true) {
matcher = PatternHtml.matcher(builder.toString());
if (matcher.find()) {
String str = matcher.group(1);
builder.replace(matcher.start(), matcher.end(), str);
continue;
}
break;
}
return builder;
}
示例8: assimilate
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* @param sequence 文本
* @param pattern 正则
* @param usedGroupIndex 使用的组号
* @param showGroupIndex 显示的组号
* @param listener 点击回掉
* @return 匹配后的文本
*/
@SuppressWarnings("all")
private static Spannable assimilate(CharSequence sequence,
Pattern pattern,
int usedGroupIndex,
int showGroupIndex,
final OnClickListener listener) {
SpannableStringBuilder builder = new SpannableStringBuilder(sequence);
Matcher matcher;
while (true) {
matcher = pattern.matcher(builder.toString());
if (matcher.find()) {
final String group0 = matcher.group(usedGroupIndex);
final String group1 = matcher.group(showGroupIndex);
builder.replace(matcher.start(), matcher.end(), group1);
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(View widget) {
listener.onClick(group0);
}
};
builder.setSpan(span, matcher.start(), matcher.start() + group1.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
continue;
}
break;
}
return builder;
}
示例9: parseOnlyAtUser
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* 格式化<a href="url" ...>@xxx</a>
* // http://my.oschina.net/u/user_id
* // http://my.oschina.net/user_ident
*
* @param context context
* @param content content
* @return Spannable
*/
static Spannable parseOnlyAtUser(final Context context, CharSequence content) {
SpannableStringBuilder builder = new SpannableStringBuilder(content);
Matcher matcher;
while (true) {
matcher = PatternAtUserWithHtml.matcher(builder.toString());
if (matcher.find()) {
final String group0 = matcher.group(1); // ident 标识 如retrofit
final String group1 = matcher.group(2); // uid id
final String group2 = matcher.group(3); // @Nick
final String group3 = matcher.group(4); // Nick
builder.replace(matcher.start(), matcher.end(), group2);
long uid;
try {
uid = group1 == null ? 0 : Integer.valueOf(group1);
} catch (Exception e) {
uid = 0;
}
final long _uid = uid;
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(View widget) {
if (_uid > 0) {
OtherUserHomeActivity.show(context, _uid);
} else if (!TextUtils.isEmpty(group0)) {
OtherUserHomeActivity.show(context, 0, group0);
} else {
OtherUserHomeActivity.show(context, group3);
}
}
};
builder.setSpan(span, matcher.start(), matcher.start() + group2.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
continue;
}
break;
}
return builder;
}
示例10: parseOnlyGist
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* 格式化代码片段标签
*/
static Spannable parseOnlyGist(final Context context, CharSequence content) {
SpannableStringBuilder builder = new SpannableStringBuilder(content);
Matcher matcher;
while (true) {
matcher = PatternGist.matcher(builder.toString());
if (matcher.find()) {
final String group1 = matcher.group(1);
final String group2 = matcher.group(2);
final String group3 = matcher.group(3);
final String group4 = matcher.group(4);
builder.replace(matcher.start(), matcher.end(), group4);
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(View widget) {
Gist gist = new Gist();
User user = new User();
user.setName(group1);
gist.setOwner(user);
gist.setId(group3);
gist.setSummary(group2);
GistDetailActivity.show(context, gist);
}
};
builder.setSpan(span, matcher.start(), matcher.start() + group4.length(),
Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
continue;
}
break;
}
return builder;
}
示例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();
}
示例12: resetText
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private void resetText() {
CharSequence workingText = fullText;
boolean ellipsized = false;
Layout layout = createWorkingLayout(workingText);
int linesCount = getLinesCount();
if (layout.getLineCount() > linesCount) {
// We have more lines of text than we are allowed to display.
workingText = fullText.subSequence(0, layout.getLineEnd(linesCount - 1));
while (createWorkingLayout(workingText + ELLIPSIS).getLineCount() > linesCount) {
int lastSpace = workingText.toString().lastIndexOf(' ');
if (lastSpace == -1) {
break;
}
workingText = workingText.subSequence(0, lastSpace);
}
// We should do this in the loop above, but it's cheaper this way.
if (workingText instanceof Spannable) {
SpannableStringBuilder builder = new SpannableStringBuilder(workingText);
Matcher matcher = endPunctuationPattern.matcher(workingText);
if (matcher.find()) {
builder.replace(matcher.start(), workingText.length(), ELLIPSIS);
}
workingText = builder;
} else {
workingText = endPunctuationPattern.matcher(workingText).replaceFirst("");
workingText = workingText + ELLIPSIS;
}
ellipsized = true;
}
if (!workingText.equals(getText())) {
programmaticChange = true;
try {
setText(workingText);
} finally {
programmaticChange = false;
}
}
isStale = false;
if (ellipsized != isEllipsized) {
isEllipsized = ellipsized;
for (EllipsizeListener listener : ellipsizeListeners) {
listener.ellipsizeStateChanged(ellipsized);
}
}
}
示例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);
}
示例14: 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);
}