本文整理汇总了Java中android.text.style.BackgroundColorSpan类的典型用法代码示例。如果您正苦于以下问题:Java BackgroundColorSpan类的具体用法?Java BackgroundColorSpan怎么用?Java BackgroundColorSpan使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BackgroundColorSpan类属于android.text.style包,在下文中一共展示了BackgroundColorSpan类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: spanTest
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
private void spanTest() {
SpannableString spannableString = new SpannableString("欢迎光临我的博客");
// 字体颜色span
ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.BLUE);
// 背景颜色span
BackgroundColorSpan backgroundColorSpan = new BackgroundColorSpan(Color.RED);
// 1,3其实是两个位置,末尾不包括
// Flag 为 span如果新增了文字 的前面不包括样式,后面包括样式
spannableString.setSpan(colorSpan, 1, 3, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
spannableString.setSpan(backgroundColorSpan, 5, 7, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder();
spannableStringBuilder.append(spannableString);
editText.setText(spannableString);
}
示例2: disableOnlyFullAppPrefs
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
private void disableOnlyFullAppPrefs() {
String fullOnly = " FULL ONLY ";
int color = Utils.adjustAlpha(CurrentTheme.getColorAccent(getActivity()), 100);
for (String name : AppPrefs.ONLY_FULL_APP_PREFS) {
Preference preference = findPreference(name);
if (preference != null) {
preference.setEnabled(false);
CharSequence summary = TextUtils.isEmpty(preference.getTitle()) ? "" : preference.getTitle();
summary = fullOnly + " " + summary;
Spannable spannable = SpannableStringBuilder.valueOf(summary);
BackgroundColorSpan span = new BackgroundColorSpan(color);
ForegroundColorSpan span1 = new ForegroundColorSpan(Color.WHITE);
spannable.setSpan(span, 0, fullOnly.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(span1, 0, fullOnly.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
preference.setTitle(spannable);
}
}
}
示例3: drawBackgroundColor
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
private void drawBackgroundColor(Canvas canvas) {
if (!(text instanceof Spanned)) {
return;
}
Spanned spanned = (Spanned) text;
BackgroundColorSpan[] spans = spanned.getSpans(start, end, BackgroundColorSpan.class);
if (spans == null || spans.length == 0) {
return;
}
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
for (BackgroundColorSpan span : spans) {
RectF spanRect = getRect(span);
if (spanRect == null) {
continue;
}
spanRect.offset(originX, originY);
paint.setColor(span.getBackgroundColor());
canvas.drawRect(spanRect, paint);
}
}
示例4: getformatedSearchSnippet
import android.text.style.BackgroundColorSpan; //导入依赖的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;
}
示例5: handleTagNode
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
@Override public void handleTagNode(TagNode node, SpannableStringBuilder builder, int start, int end) {
if (isPre) {
StringBuffer buffer = new StringBuffer();
buffer.append("\n");//fake padding top + make sure, pre is always by itself
getPlainText(buffer, node);
buffer.append("\n");//fake padding bottom + make sure, pre is always by itself
builder.append(replace(buffer.toString()));
builder.append("\n");
builder.setSpan(new CodeBackgroundRoundedSpan(color), start, builder.length(), SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append("\n");
this.appendNewLine(builder);
this.appendNewLine(builder);
} else {
StringBuffer text = node.getText();
builder.append(" ");
builder.append(replace(text.toString()));
builder.append(" ");
final int stringStart = start + 1;
final int stringEnd = builder.length() - 1;
builder.setSpan(new BackgroundColorSpan(color), stringStart, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE);
if (theme == PrefGetter.LIGHT) {
builder.setSpan(new ForegroundColorSpan(Color.RED), stringStart, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE);
}
builder.setSpan(new TypefaceSpan("monospace"), stringStart, stringEnd, SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
示例6: setHighlight
import android.text.style.BackgroundColorSpan; //导入依赖的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
}
}
}
示例7: hasBackgroundSpanOn
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
public static Matcher<View> hasBackgroundSpanOn(final String text, @ColorRes final int colorResource) {
return new CustomTypeSafeMatcher<View>("") {
@Override
protected boolean matchesSafely(View view) {
if (view == null || !(view instanceof TextView))
return false;
SpannableString spannableString = (SpannableString) ((TextView) view).getText();
BackgroundColorSpan[] spans = spannableString.getSpans(0, spannableString.length(), BackgroundColorSpan.class);
for (BackgroundColorSpan span : spans) {
int start = spannableString.getSpanStart(span);
int end = spannableString.getSpanEnd(span);
CharSequence highlightedString = spannableString.subSequence(start, end);
if (text.equals(highlightedString.toString())) {
return span.getBackgroundColor() == view.getContext().getResources().getColor(colorResource);
}
}
return false;
}
};
}
示例8: highlight
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
/**
* Highlights everything that matched the current filter (if any) in text
*
* @param text the text to highlight
* @param color the highlight color
* @return a spannable string
*/
protected Spannable highlight(String text, int color) {
if (text == null) return null;
Spannable highlighted = new SpannableStringBuilder(text);
AdvancedFilter filter = getFilter();
if (filter == null || filter.mPattern == null){
return highlighted;
}
Matcher matcher = filter.mPattern.matcher(text);
while (matcher.find()){
highlighted.setSpan(new BackgroundColorSpan(color), matcher.start(),
matcher.end(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE
);
}
return highlighted;
}
示例9: createColorPicker
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
protected ColorListPickerDialog createColorPicker(boolean fillColor, int selectedColor) {
ColorListPickerDialog dialog = new ColorListPickerDialog(getContext());
if (!fillColor) {
dialog.setTitle(R.string.format_text_color);
} else {
dialog.setTitle(R.string.format_fill_color);
}
dialog.setSelectedColor(selectedColor);
dialog.setPositiveButton(R.string.action_cancel, null);
dialog.setOnColorChangeListener((ColorListPickerDialog d, int newColorIndex, int color) -> {
if (!fillColor) {
removeSpan(ForegroundColorSpan.class);
setSpan(new ForegroundColorSpan(color));
} else {
removeSpan(BackgroundColorSpan.class);
setSpan(new BackgroundColorSpan(color));
}
d.cancel();
});
return dialog;
}
示例10: spanToJson
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
public static JsonObject spanToJson(Object span) {
JsonObject ret = new JsonObject();
if (span instanceof ForegroundColorSpan) {
ret.addProperty("type", SPAN_TYPE_FOREGROUND);
ret.addProperty("color", ((ForegroundColorSpan) span).getForegroundColor());
} else if (span instanceof BackgroundColorSpan) {
ret.addProperty("type", SPAN_TYPE_BACKGROUND);
ret.addProperty("color", ((BackgroundColorSpan) span).getBackgroundColor());
} else if (span instanceof StyleSpan) {
ret.addProperty("type", SPAN_TYPE_STYLE);
ret.addProperty("style", ((StyleSpan) span).getStyle());
} else {
return null;
}
return ret;
}
示例11: endCssStyle
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
private void endCssStyle(String tag, Editable text) {
Strikethrough s = getLast(text, Strikethrough.class);
if (s != null) {
setSpanFromMark(tag, text, s, new StrikethroughSpan());
}
Background b = getLast(text, Background.class);
if (b != null) {
setSpanFromMark(tag, text, b, new BackgroundColorSpan(b.mBackgroundColor));
}
Foreground f = getLast(text, Foreground.class);
if (f != null) {
setSpanFromMark(tag, text, f, new ForegroundColorSpan(f.mForegroundColor));
}
AbsoluteSize a = getLast(text, AbsoluteSize.class);
if (a != null) {
setSpanFromMark(tag, text, a, new AbsoluteSizeSpan(a.getTextSize()));
}
RelativeSize r = getLast(text, RelativeSize.class);
if (r != null) {
setSpanFromMark(tag, text, r, new RelativeSizeSpan(r.getTextProportion()));
}
}
示例12: resetAutocompleteState
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
/**
* Reset autocomplete states to their initial values
*/
private void resetAutocompleteState() {
mAutoCompleteSpans = new Object[] {
// Span to mark the autocomplete text
AUTOCOMPLETE_SPAN,
// Span to change the autocomplete text color
new BackgroundColorSpan(
ContextCompat.getColor(getContext(), R.color.colorAccent))
};
mAutoCompleteResult = AutocompleteResult.emptyResult();
// Pretend we already autocompleted the existing text,
// so that actions like backspacing don't trigger autocompletion.
mAutoCompletePrefixLength = getText().length();
// Show the cursor.
setCursorVisible(true);
}
示例13: setComposingTextInternalWithBackgroundColor
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
/**
* Equivalent to {@link #setComposingTextInternal(CharSequence, int)} except that this method
* allows to set {@link BackgroundColorSpan} to the composing text with the given color.
*
* <p>TODO: Currently the background color is exclusive with the black underline, which is
* automatically added by the framework. We need to change the framework if we need to have both
* of them at the same time.</p>
* <p>TODO: Should we move this method to {@link RichInputConnection}?</p>
*
* @param newComposingText the composing text to be set
* @param newCursorPosition the new cursor position
* @param backgroundColor the background color to be set to the composing text. Set
* {@link Color#TRANSPARENT} to disable the background color.
* @param coloredTextLength the length of text, in Java chars, which should be rendered with
* the given background color.
*/
private void setComposingTextInternalWithBackgroundColor(final CharSequence newComposingText,
final int newCursorPosition, final int backgroundColor, final int coloredTextLength) {
final CharSequence composingTextToBeSet;
if (backgroundColor == Color.TRANSPARENT) {
composingTextToBeSet = newComposingText;
} else {
final SpannableString spannable = new SpannableString(newComposingText);
final BackgroundColorSpan backgroundColorSpan =
new BackgroundColorSpan(backgroundColor);
final int spanLength = Math.min(coloredTextLength, spannable.length());
spannable.setSpan(backgroundColorSpan, 0, spanLength,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE | Spanned.SPAN_COMPOSING);
composingTextToBeSet = spannable;
}
mConnection.setComposingText(composingTextToBeSet, newCursorPosition);
}
示例14: replace
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
/**
* 将匹配的字符设置背景色
*
* @param target 目标字符
* @param content 全体字符
* @return 设置好背景色的text
*/
public Spannable replace(String target, String content) {
Spannable spanText = new SpannableString(content);
// int index = -1;
for (Integer i : searchResult) {
spanText.setSpan(new BackgroundColorSpan(getResources().getColor(R.color.minionYellow))
, i, i + target.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
// while (content.indexOf(target, index + 1) != -1) {
// index = content.indexOf(target, index + 1);
// spanText.setSpan(new BackgroundColorSpan(getResources().getColor(R.color.minionYellow))
// , index, index + target.length(),
// Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// }
return spanText;
}
示例15: processNoIntralineDataA
import android.text.style.BackgroundColorSpan; //导入依赖的package包/类
private void processNoIntralineDataA(
DiffContentInfo diff, DiffInfoModel m, String line, int color) {
final Spannable.Factory spannableFactory = Spannable.Factory.getInstance();
if (diff.a != null && diff.b != null
&& diff.a.length == 1 && diff.b.length == 1) {
Spannable span = spannableFactory.newSpannable(prepareTabs(line));
int z = diff.a[0].indexOf(diff.b[0]);
if (z != -1) {
if (z > 0) {
span.setSpan(new BackgroundColorSpan(color),
0, z, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
if (z + diff.b[0].length() < diff.a[0].length()) {
z = z + diff.b[0].length();
span.setSpan(new BackgroundColorSpan(color),
z, diff.a[0].length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
m.lineA = span;
} else {
m.lineA = prepareTabs(line);
}
}