本文整理匯總了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);
}