本文整理汇总了Java中android.text.SpannableString类的典型用法代码示例。如果您正苦于以下问题:Java SpannableString类的具体用法?Java SpannableString怎么用?Java SpannableString使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SpannableString类属于android.text包,在下文中一共展示了SpannableString类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getDesignationText
import android.text.SpannableString; //导入依赖的package包/类
public SpannableString getDesignationText()
{
String text = txtDesignation.getText().toString();
SpannableString s = new SpannableString(text);
s.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.colorAccent)), text.indexOf("@"), text.length(), 0);
/*
s.setSpan(new RelativeSizeSpan(1.2f), text.indexOf("A"), text.length(), 0);
s.setSpan(new StyleSpan(Typeface.NORMAL), text.indexOf("A"), text.length(), 0);
s.setSpan(new ForegroundColorSpan(Color.BLACK), text.indexOf("A"), text.length(), 0);
*/
return s;
}
示例2: spanTest
import android.text.SpannableString; //导入依赖的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);
}
示例3: getformatedSearchSnippet
import android.text.SpannableString; //导入依赖的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;
}
示例4: getTeaser
import android.text.SpannableString; //导入依赖的package包/类
public static SpannableStringBuilder getTeaser(Context ctx, Spanned body) {
if (body.length() < TEASER_LENGTH)
throw new IllegalArgumentException(
"String is shorter than TEASER_LENGTH");
SpannableStringBuilder builder =
new SpannableStringBuilder(body.subSequence(0, TEASER_LENGTH));
String ellipsis = ctx.getString(R.string.ellipsis);
builder.append(ellipsis).append(" ");
Spannable readMore = new SpannableString(
ctx.getString(R.string.read_more) + ellipsis);
ForegroundColorSpan fg = new ForegroundColorSpan(
ContextCompat.getColor(ctx, R.color.briar_text_link));
readMore.setSpan(fg, 0, readMore.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
builder.append(readMore);
return builder;
}
示例5: show
import android.text.SpannableString; //导入依赖的package包/类
static void show(View parent,
CharSequence text,
int duration,
@ColorInt int textColor,
@ColorInt int bgColor,
CharSequence actionText,
@ColorInt int actionTextColor,
View.OnClickListener listener) {
SpannableString spannableString = new SpannableString(text);
ForegroundColorSpan colorSpan = new ForegroundColorSpan(textColor);
spannableString.setSpan(colorSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, spannableString, duration));
Snackbar snackbar = snackbarWeakReference.get();
View view = snackbar.getView();
view.setBackgroundColor(bgColor);
if (actionText != null && actionText.length() > 0 && listener != null) {
snackbar.setActionTextColor(actionTextColor);
snackbar.setAction(actionText, listener);
}
snackbar.show();
}
示例6: convert
import android.text.SpannableString; //导入依赖的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: render
import android.text.SpannableString; //导入依赖的package包/类
public void render(final TextView textView, final Context context, String line)
{
final Handler handler = new Handler(Looper.getMainLooper());
final SpannableString text = new SpannableString(" " + line + "\n");
handler.post(new Runnable()
{
@Override
public void run()
{
//set the image tag behind (left of) the text
text.setSpan(new ImageSpan(context, IMAGE_TAG), 0, 1, 0);
textView.append(text);
}
});
}
示例8: showDialog
import android.text.SpannableString; //导入依赖的package包/类
@Override
public void showDialog(Activity activity) {
super.showDialog(activity);
dismissDialog(activity);
ThemedAlertDialog.Builder dialog = new ThemedAlertDialog.Builder(activity);
dialog.setTitle(R.string.connection_error_command_title);
StringBuilder commands = new StringBuilder();
for (String cmd : mCommands) {
commands.append('/');
commands.append(cmd);
commands.append('\n');
}
SpannableString commandsSeq = new SpannableString(commands);
commandsSeq.setSpan(new TypefaceSpan("monospace"), 0, commandsSeq.length(),
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
dialog.setMessage(SpannableStringHelper.format(activity.getResources().getQuantityText(
R.plurals.connection_error_command_dialog_content, mCommands.size()),
mNetworkName, commandsSeq));
dialog.setPositiveButton(R.string.action_ok, null);
dialog.setOnDismissListener((DialogInterface di) -> {
dismiss();
});
mDialog = dialog.show();
}
示例9: terminateToken
import android.text.SpannableString; //导入依赖的package包/类
@Override
public CharSequence terminateToken(CharSequence text) {
int i = text.length();
while (i > 0 && text.charAt(i - 1) == ' ') {
i--;
}
if (i > 0 && text.charAt(i - 1) == ' ') {
return text;
} else if (text instanceof Spanned) {
SpannableString s = new SpannableString(text + " ");
TextUtils.copySpansFrom((Spanned) text, 0, text.length(), Object.class, s, 0);
return s;
} else {
return text + " ";
}
}
示例10: show
import android.text.SpannableString; //导入依赖的package包/类
/**
* 设置snackbar文字和背景颜色
*
* @param parent 父视图(CoordinatorLayout或者DecorView)
* @param text 文本
* @param duration 显示时长
* @param textColor 文本颜色
* @param bgColor 背景色
* @param actionText 事件文本
* @param actionTextColor 事件文本颜色
* @param listener 监听器
*/
private static void show(View parent,
CharSequence text,
int duration,
@ColorInt int textColor,
@ColorInt int bgColor,
CharSequence actionText,
@ColorInt int actionTextColor,
View.OnClickListener listener) {
SpannableString spannableString = new SpannableString(text);
ForegroundColorSpan colorSpan = new ForegroundColorSpan(textColor);
spannableString.setSpan(colorSpan, 0, spannableString.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
snackbarWeakReference = new WeakReference<>(Snackbar.make(parent, spannableString, duration));
Snackbar snackbar = snackbarWeakReference.get();
View view = snackbar.getView();
view.setBackgroundColor(bgColor);
if (actionText != null && actionText.length() > 0 && listener != null) {
snackbar.setActionTextColor(actionTextColor);
snackbar.setAction(actionText, listener);
}
snackbar.show();
}
示例11: newColor
import android.text.SpannableString; //导入依赖的package包/类
private void newColor(int color){
this.color = color;
// Sets action bar colors
if (getSupportActionBar() == null) return;
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xFF000000 | color));
boolean dark = Color.red(color) * 0.299 + Color.green(color) * 0.587 + Color.blue(color) * 0.114 < 180;
SpannableString s = new SpannableString(getSupportActionBar().getTitle());
s.setSpan(new ForegroundColorSpan(dark ? Color.WHITE : Color.BLACK), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getSupportActionBar().setTitle(s);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
float[] hsv = new float[3];
Color.colorToHSV(color, hsv);
hsv[2] *= 0.75;
getWindow().setStatusBarColor(Color.HSVToColor(hsv));
}
}
示例12: displayPlaylistInfo
import android.text.SpannableString; //导入依赖的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: setText
import android.text.SpannableString; //导入依赖的package包/类
public void setText(String text) {
int startIndex = 0;
while (true) {
int start = text.indexOf("《");
int end = text.indexOf("》");
if (start < 0 || end < 0) {
append(text.substring(startIndex));
break;
}
append(text.substring(startIndex, start));
SpannableString spanableInfo = new SpannableString(text.substring(start, end + 1));
spanableInfo.setSpan(new Clickable(spanableInfo.toString()), 0, end + 1 - start, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
append(spanableInfo);
//setMovementMethod()该方法必须调用,否则点击事件不响应
setMovementMethod(LinkMovementMethod.getInstance());
text = text.substring(end + 1);
LogUtils.e(spanableInfo.toString());
}
}
示例14: setHint
import android.text.SpannableString; //导入依赖的package包/类
public void setHint(@NonNull String hint, @Nullable CharSequence subHint) {
this.hint = new SpannableString(hint);
this.hint.setSpan(new RelativeSizeSpan(0.8f), 0, hint.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
if (subHint != null) {
this.subHint = new SpannableString(subHint);
this.subHint.setSpan(new RelativeSizeSpan(0.8f), 0, subHint.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
} else {
this.subHint = null;
}
if (this.subHint != null) {
super.setHint(new SpannableStringBuilder().append(ellipsizeToWidth(this.hint))
.append("\n")
.append(ellipsizeToWidth(this.subHint)));
} else {
super.setHint(ellipsizeToWidth(this.hint));
}
}
示例15: generateState
import android.text.SpannableString; //导入依赖的package包/类
private CharSequence generateState(OrgHead head) {
SpannableString str = new SpannableString(head.getState());
ForegroundColorSpan color;
if (AppPreferences.todoKeywordsSet(mContext).contains(head.getState())) {
color = attributes.colorTodo;
} else if (AppPreferences.doneKeywordsSet(mContext).contains(head.getState())) {
color = attributes.colorDone;
} else {
color = attributes.colorUnknown;
}
str.setSpan(color, 0, str.length(), 0);
return str;
}