本文整理汇总了Java中android.text.Spannable.setSpan方法的典型用法代码示例。如果您正苦于以下问题:Java Spannable.setSpan方法的具体用法?Java Spannable.setSpan怎么用?Java Spannable.setSpan使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类android.text.Spannable
的用法示例。
在下文中一共展示了Spannable.setSpan方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: applyLink
import android.text.Spannable; //导入方法依赖的package包/类
private static final void applyLink(String url, int start, int end, Spannable text, final ColorStateList linkColor, final ColorStateList bgColor, QMUIOnSpanClickListener l) {
text.setSpan(new StyleableURLSpan(url, l) {
@Override
public void updateDrawState(TextPaint ds) {
if (linkColor != null) {
int normalLinkColor = linkColor.getColorForState(new int[]{android.R.attr.state_enabled, -android.R.attr.state_pressed}, Color.TRANSPARENT);
int pressedLinkColor = linkColor.getColorForState(new int[]{android.R.attr.state_pressed}, normalLinkColor);
ds.linkColor = mPressed ? pressedLinkColor : normalLinkColor;
}
if (bgColor != null) {
int normalBgColor = bgColor.getColorForState(new int[]{android.R.attr.state_enabled, -android.R.attr.state_pressed}, Color.TRANSPARENT);
int pressedBgColor = bgColor.getColorForState(new int[]{android.R.attr.state_pressed}, normalBgColor);
ds.bgColor = mPressed ? pressedBgColor : normalBgColor;
}
super.updateDrawState(ds);
ds.setUnderlineText(false);
}
}, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
示例2: getTeaser
import android.text.Spannable; //导入方法依赖的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;
}
示例3: emoticonDisplay
import android.text.Spannable; //导入方法依赖的package包/类
public static void emoticonDisplay(Context context, Spannable spannable, String emoticonName, int fontSize, int start, int end) {
Drawable drawable = getDrawableFromAssets(context, emoticonName);
if (drawable != null) {
int itemHeight;
int itemWidth;
if (fontSize == WRAP_DRAWABLE) {
itemHeight = drawable.getIntrinsicHeight();
itemWidth = drawable.getIntrinsicWidth();
} else {
itemHeight = fontSize;
itemWidth = fontSize;
}
drawable.setBounds(0, 0, itemHeight, itemWidth);
EmoticonSpan imageSpan = new EmoticonSpan(drawable);
spannable.setSpan(imageSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
示例4: emoticonDisplay
import android.text.Spannable; //导入方法依赖的package包/类
public static void emoticonDisplay(Context context, Spannable spannable, int emoticon, int fontSize, int start, int end) {
Drawable drawable = getDrawable(context, emoticon);
if (drawable != null) {
int itemHeight;
int itemWidth;
if (fontSize == WRAP_DRAWABLE) {
itemHeight = drawable.getIntrinsicHeight();
itemWidth = drawable.getIntrinsicWidth();
} else {
itemHeight = fontSize;
itemWidth = fontSize;
}
drawable.setBounds(0, 0, itemHeight, itemWidth);
EmoticonSpan imageSpan = new EmoticonSpan(drawable);
spannable.setSpan(imageSpan, start, end, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
示例5: setSpanFromMark
import android.text.Spannable; //导入方法依赖的package包/类
private void setSpanFromMark(String tag, Spannable text, Object mark, Object... spans) {
int where = text.getSpanStart(mark);
text.removeSpan(mark);
int len = text.length();
if (where != len) {
for (Object span : spans) {
if (mSpanCallback != null) {
span = mSpanCallback.onSpanCreated(tag, span);
}
text.setSpan(span, where, len, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
示例6: addSmiles
import android.text.Spannable; //导入方法依赖的package包/类
/**
* replace existing spannable with smiles
* @param context
* @param spannable
* @return
*/
public static boolean addSmiles(Context context, Spannable spannable) {
boolean hasChanges = false;
for (Map.Entry<Pattern, Object> entry : emoticons.entrySet()) {
Matcher matcher = entry.getKey().matcher(spannable);
while (matcher.find()) {
boolean set = true;
for (ImageSpan span : spannable.getSpans(matcher.start(),
matcher.end(), ImageSpan.class))
if (spannable.getSpanStart(span) >= matcher.start()
&& spannable.getSpanEnd(span) <= matcher.end())
spannable.removeSpan(span);
else {
set = false;
break;
}
if (set) {
hasChanges = true;
Object value = entry.getValue();
if(value instanceof String && !((String) value).startsWith("http")){
File file = new File((String) value);
if(!file.exists() || file.isDirectory()){
return false;
}
spannable.setSpan(new ImageSpan(context, Uri.fromFile(file)),
matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else{
spannable.setSpan(new ImageSpan(context, (Integer)value),
matcher.start(), matcher.end(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
}
return hasChanges;
}
示例7: setCompanyName
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public void setCompanyName(String name) {
String companyName = getString(R.string.company_name) + "\n" + name;
Spannable spannable = new SpannableStringBuilder(companyName);
spannable.setSpan(new StyleSpan(Typeface.BOLD), 0, companyName.length() - name.length() - 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new StyleSpan(Typeface.NORMAL), companyName.length() - name.length(), companyName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textViewCompanyName.setText(spannable);
}
示例8: onTakeFocus
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public void onTakeFocus(TextView view, Spannable text, int dir) {
Selection.removeSelection(text);
if ((dir & View.FOCUS_BACKWARD) != 0) {
text.setSpan(FROM_BELOW, 0, 0, Spannable.SPAN_POINT_POINT);
} else {
text.removeSpan(FROM_BELOW);
}
}
示例9: bindTitle
import android.text.Spannable; //导入方法依赖的package包/类
private void bindTitle(TitleViewHolder holder) {
if (mLayoutManager instanceof StaggeredGridLayoutManager) {
StaggeredGridLayoutManager.LayoutParams layoutParams = new StaggeredGridLayoutManager.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.setFullSpan(true);
holder.itemView.setLayoutParams(layoutParams);
}
final int position = holder.getAdapterPosition();
if (position == 0) {
String title = mContext.getString(getTypeTitle());
String fullText = mContext.getString(R.string.who_can_have_access) + " " + title;
Spannable spannable = SpannableStringBuilder.valueOf(fullText);
ClickableSpan span = new ClickableSpan() {
@Override
public void onClick(View widget) {
if(mActionListener != null){
mActionListener.onTypeClick();
}
}
};
spannable.setSpan(span, fullText.length() - title.length(), fullText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
holder.title.setText(spannable, TextView.BufferType.SPANNABLE);
} else {
holder.title.setText(R.string.who_cannot_have_access);
}
holder.buttonAdd.setOnClickListener(v -> {
if(mActionListener != null){
if(position == 0){
mActionListener.onAddToAllowedClick();
} else {
mActionListener.onAddToDisallowedClick();
}
}
});
}
示例10: onViewCreated
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
toolbar.getNavigationIcon().setColorFilter(
getResources().getColor(R.color.icon_grey), PorterDuff.Mode.SRC_ATOP);
toolbar.setNavigationOnClickListener(v -> getActivity().onBackPressed());
int padding = getResources().getDimensionPixelSize(R.dimen.about_header_padding);
bannerImage.setPadding(padding, padding, padding, padding);
Glide.with(this)
.load(R.drawable.egg)
.into(bannerImage);
Spannable span = new SpannableString(title.getContext().getString(R.string.about_title));
span.setSpan(new TextAppearanceSpan(title.getContext(),
R.style.ProteinTextAppearance_Regular_XXL_Primary), 0, 7,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
span.setSpan(new TextAppearanceSpan(title.getContext(),
R.style.ProteinTextAppearance_Regular_L_Hint), 7, span.length(),
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
title.setText(span);
versionName.setText(versionName.getContext().getString(R.string.about_version, BuildConfig.VERSION_NAME));
layoutGe.setOnClickListener(aboutOnClickListener);
layoutMelodie.setOnClickListener(aboutOnClickListener);
layoutOthers.setOnClickListener(aboutOnClickListener);
versionLayout.setOnClickListener(aboutOnClickListener);
licenseLayout.setOnClickListener(aboutOnClickListener);
shareLayout.setOnClickListener(aboutOnClickListener);
}
示例11: setAdvancementText
import android.text.Spannable; //导入方法依赖的package包/类
private void setAdvancementText(int value, PopulationType.Civilization civilization) {
String advanceTextStart = getActivity().getString(R.string.advance_to);
String advanceName = getPopulationTypeByProgress(value, civilization).getString(getContext());
if (advancementText != null) {
advancementText.setText(advanceTextStart + " " + advanceName.toLowerCase(), TextView.BufferType.SPANNABLE);
Spannable sp = (Spannable) advancementText.getText();
sp.setSpan(new StyleSpan(Typeface.BOLD), advanceTextStart.length(), 1 + advanceTextStart.length() + advanceName.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
示例12: onTouchEvent
import android.text.Spannable; //导入方法依赖的package包/类
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
int action = event.getAction();
if(action == MotionEvent.ACTION_DOWN){
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
mClickLinks = buffer.getSpans(off, off, ClickableSpan.class);
if(mClickLinks.length > 0){
// 点击的是Span区域,不要把点击事件传递
setPassToTv(false);
Selection.setSelection(buffer,
buffer.getSpanStart(mClickLinks[0]),
buffer.getSpanEnd(mClickLinks[0]));
//设置点击区域的背景色
mBgSpan = new BackgroundColorSpan(clickableSpanBgClor);
buffer.setSpan(mBgSpan,
buffer.getSpanStart(mClickLinks[0]),
buffer.getSpanEnd(mClickLinks[0]),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}else{
setPassToTv(true);
// textview选中效果
widget.setBackgroundColor(textViewBgColor);
}
}else if(action == MotionEvent.ACTION_UP){
if(mClickLinks.length > 0){
mClickLinks[0].onClick(widget);
if(mBgSpan != null){//移除点击时设置的背景span
buffer.removeSpan(mBgSpan);
}
}else{
}
Selection.removeSelection(buffer);
widget.setBackgroundColor(Color.TRANSPARENT);
}else if(action == MotionEvent.ACTION_MOVE){
//这种情况不用做处理
}else{
if(mBgSpan != null){//移除点击时设置的背景span
buffer.removeSpan(mBgSpan);
}
widget.setBackgroundColor(Color.TRANSPARENT);
}
return Touch.onTouchEvent(widget, buffer, event);
}
示例13: openTags
import android.text.Spannable; //导入方法依赖的package包/类
/**
* Iterates over an array of tags and applies them to the beginning of the specified
* Spannable object so that future text appended to the text will have the styling
* applied to it. Do not call this method directly.
*/
private static void openTags(Spannable text, Object[] tags) {
for (Object tag : tags) {
text.setSpan(tag, 0, 0, Spannable.SPAN_MARK_MARK);
}
}
示例14: getDisplayContents
import android.text.Spannable; //导入方法依赖的package包/类
@Override
public CharSequence getDisplayContents() {
AddressBookParsedResult result = (AddressBookParsedResult) getResult();
StringBuilder contents = new StringBuilder(100);
ParsedResult.maybeAppend(result.getNames(), contents);
int namesLength = contents.length();
String pronunciation = result.getPronunciation();
if (pronunciation != null && !pronunciation.isEmpty()) {
contents.append("\n(");
contents.append(pronunciation);
contents.append(')');
}
ParsedResult.maybeAppend(result.getTitle(), contents);
ParsedResult.maybeAppend(result.getOrg(), contents);
ParsedResult.maybeAppend(result.getAddresses(), contents);
String[] numbers = result.getPhoneNumbers();
if (numbers != null) {
for (String number : numbers) {
if (number != null) {
ParsedResult.maybeAppend(
PhoneNumberUtils.formatNumber(number), contents);
}
}
}
ParsedResult.maybeAppend(result.getEmails(), contents);
ParsedResult.maybeAppend(result.getURLs(), contents);
String birthday = result.getBirthday();
if (birthday != null && !birthday.isEmpty()) {
Date date = parseDate(birthday);
if (date != null) {
ParsedResult.maybeAppend(
DateFormat.getDateInstance(DateFormat.MEDIUM).format(
date.getTime()), contents);
}
}
ParsedResult.maybeAppend(result.getNote(), contents);
if (namesLength > 0) {
// Bold the full name to make it stand out a bit.
Spannable styled = new SpannableString(contents.toString());
styled.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
namesLength, 0);
return styled;
} else {
return contents.toString();
}
}
示例15: lavFedSkriftTil
import android.text.Spannable; //导入方法依赖的package包/类
protected static Spannable lavFedSkriftTil(String tekst, int fedTil) {
Spannable spannable = new SpannableString(tekst);
spannable.setSpan(App.skrift_gibson_fed_span, 0, fedTil, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable;
}