本文整理汇总了Java中android.text.SpannableStringBuilder.delete方法的典型用法代码示例。如果您正苦于以下问题:Java SpannableStringBuilder.delete方法的具体用法?Java SpannableStringBuilder.delete怎么用?Java SpannableStringBuilder.delete使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.SpannableStringBuilder
的用法示例。
在下文中一共展示了SpannableStringBuilder.delete方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: trimText
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* Trims the specified text by removing white spaces at its start and also at its end.
*
* @param text The text to be trimmed.
* @return Trimmed text.
*/
@NonNull
public static CharSequence trimText(@NonNull CharSequence text) {
int length = text.length();
final int trimmedLength = TextUtils.getTrimmedLength(text);
if (length > trimmedLength) {
final SpannableStringBuilder builder = new SpannableStringBuilder(text);
// Remove white spaces from the start.
int start = 0;
while (start < length && builder.charAt(start) <= ' ') {
start++;
}
builder.delete(0, start);
length -= start;
// Remove white spaces from the end.
int end = length;
while (end >= 0 && builder.charAt(end - 1) <= ' ') {
end--;
}
builder.delete(end, length);
return builder;
}
return text;
}
示例2: setText
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
@Override
public void setText(CharSequence text, BufferType type) {
if (!TextUtils.isEmpty(text)) {
Matcher matcher = PATTERN_SEARCH_TERM.matcher(text);
SpannableStringBuilder ssb = new SpannableStringBuilder(text);
List<String> hits = new ArrayList<>();
while (matcher.find()) {
hits.add(matcher.group(1));
}
for (String hit : hits) {
int start = ssb.toString().indexOf(hit);
int end = start + hit.length();
ssb.setSpan(new StyleSpan(Typeface.BOLD), start, end, 0);
// delete the markup tokens
ssb.delete(end - 1, end);
ssb.delete(start, start + 1);
}
text = ssb;
}
super.setText(text, type);
}
示例3: buildStyledSnippet
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* Given a snippet string with matching segments surrounded by curly
* braces, turn those areas into bold spans, removing the curly braces.
*/
public static Spannable buildStyledSnippet(String snippet) {
final SpannableStringBuilder builder = new SpannableStringBuilder(snippet);
// Walk through string, inserting bold snippet spans
int startIndex, endIndex = -1, delta = 0;
while ((startIndex = snippet.indexOf('{', endIndex)) != -1) {
endIndex = snippet.indexOf('}', startIndex);
// Remove braces from both sides
builder.delete(startIndex - delta, startIndex - delta + 1);
builder.delete(endIndex - delta - 1, endIndex - delta);
// Insert bold style
builder.setSpan(new StyleSpan(Typeface.BOLD),
startIndex - delta, endIndex - delta - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//builder.setSpan(new ForegroundColorSpan(0xff111111),
// startIndex - delta, endIndex - delta - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
delta += 2;
}
return builder;
}
示例4: setStrikethrough
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private void setStrikethrough(SpannableStringBuilder builder) {
final int offset = "[[d[".length(); // == "]d]]".length()
int start = -1;
int end;
for (int i = 0; i < builder.length() - 3; i++) {
if (builder.charAt(i) == '['
&& builder.charAt(i + 1) == '['
&& builder.charAt(i + 2) == 'd'
&& builder.charAt(i + 3) == '[') {
start = i + offset;
} else if (builder.charAt(i) == ']'
&& builder.charAt(i + 1) == 'd'
&& builder.charAt(i + 2) == ']'
&& builder.charAt(i + 3) == ']') {
end = i;
builder.setSpan(new StrikethroughSpan(), start, end,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
builder.delete(end, end + offset);
builder.delete(start - offset, start);
i -= offset + (end - start); // length of text
}
}
}
示例5: setHighlight
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private void setHighlight(SpannableStringBuilder builder, String subreddit) {
final int offset = "[[h[".length(); // == "]h]]".length()
int start = -1;
int end;
for (int i = 0; i < builder.length() - 4; i++) {
if (builder.charAt(i) == '['
&& builder.charAt(i + 1) == '['
&& builder.charAt(i + 2) == 'h'
&& builder.charAt(i + 3) == '[') {
start = i + offset;
} else if (builder.charAt(i) == ']'
&& builder.charAt(i + 1) == 'h'
&& builder.charAt(i + 2) == ']'
&& builder.charAt(i + 3) == ']') {
end = i;
builder.setSpan(new BackgroundColorSpan(Palette.getColor(subreddit)), start, end,
Spanned.SPAN_INCLUSIVE_INCLUSIVE);
builder.delete(end, end + offset);
builder.delete(start - offset, start);
i -= offset + (end - start); // length of text
}
}
}
示例6: convert
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
public static Spanned convert(String input) {
SpannableStringBuilder spanned = fromMD(PatternMatcher.findUserTags(input));
try {
if(input != null && !input.isEmpty()) {
while (spanned.charAt(spanned.length() - 1) == '\n') {
spanned = spanned.delete(spanned.length() - 1, spanned.length());
}
}
} catch (Exception e){
e.printStackTrace();
}
return spanned;
}
示例7: setCodeFont
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
/**
* Sets the styling for string with code segments. <p/> The general process is to search for
* <code>[[<[</code> and <code>]>]]</code> tokens to find the code fragments within the
* escaped text. A <code>Spannable</code> is created which which breaks up the origin sequence
* into non-code and code fragments, and applies a monospace font to the code fragments.
*
* @param sequence the Spannable generated from Html.fromHtml
* @return the message with monospace font applied to code fragments
*/
private SpannableStringBuilder setCodeFont(SpannableStringBuilder sequence) {
int start = 0;
int end = 0;
for (int i = 0; i < sequence.length(); i++) {
if (sequence.charAt(i) == '[' && i < sequence.length() - 3) {
if (sequence.charAt(i + 1) == '['
&& sequence.charAt(i + 2) == '<'
&& sequence.charAt(i + 3) == '[') {
start = i;
}
} else if (sequence.charAt(i) == ']' && i < sequence.length() - 3) {
if (sequence.charAt(i + 1) == '>'
&& sequence.charAt(i + 2) == ']'
&& sequence.charAt(i + 3) == ']') {
end = i;
}
}
if (end > start) {
sequence.delete(end, end + 4);
sequence.delete(start, start + 4);
sequence.setSpan(new TypefaceSpan("monospace"), start, end - 4,
Spannable.SPAN_INCLUSIVE_INCLUSIVE);
start = 0;
end = 0;
i = i - 4; // move back to compensate for removal of [[<[
}
}
return sequence;
}
示例8: removeUrlFromEditable
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private static Editable removeUrlFromEditable(Editable editable, @Nullable URLSpan urlSpan) {
if (urlSpan == null) {
return editable;
}
SpannableStringBuilder builder = new SpannableStringBuilder(editable);
int start = builder.getSpanStart(urlSpan);
int end = builder.getSpanEnd(urlSpan);
if (start != -1 && end != -1) {
builder.delete(start, end);
}
return builder;
}
示例9: cleanUpText
import android.text.SpannableStringBuilder; //导入方法依赖的package包/类
private SpannableStringBuilder cleanUpText(SpannableStringBuilder builder) {
// Having joined the text elements, we need to do some final cleanup on the result.
// 1. Collapse multiple consecutive spaces into a single space.
int builderLength = builder.length();
for (int i = 0; i < builderLength; i++) {
if (builder.charAt(i) == ' ') {
int j = i + 1;
while (j < builder.length() && builder.charAt(j) == ' ') {
j++;
}
int spacesToDelete = j - (i + 1);
if (spacesToDelete > 0) {
builder.delete(i, i + spacesToDelete);
builderLength -= spacesToDelete;
}
}
}
// 2. Remove any spaces from the start of each line.
if (builderLength > 0 && builder.charAt(0) == ' ') {
builder.delete(0, 1);
builderLength--;
}
for (int i = 0; i < builderLength - 1; i++) {
if (builder.charAt(i) == '\n' && builder.charAt(i + 1) == ' ') {
builder.delete(i + 1, i + 2);
builderLength--;
}
}
// 3. Remove any spaces from the end of each line.
if (builderLength > 0 && builder.charAt(builderLength - 1) == ' ') {
builder.delete(builderLength - 1, builderLength);
builderLength--;
}
for (int i = 0; i < builderLength - 1; i++) {
if (builder.charAt(i) == ' ' && builder.charAt(i + 1) == '\n') {
builder.delete(i, i + 1);
builderLength--;
}
}
// 4. Trim a trailing newline, if there is one.
if (builderLength > 0 && builder.charAt(builderLength - 1) == '\n') {
builder.delete(builderLength - 1, builderLength);
/*builderLength--;*/
}
return builder;
}