android.text.SpannableString类的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(9.8k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中android.text.SpannableString类的一些代码示例,展示了SpannableString类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SpannableString类的具体详情如下:
包路径:android.text.SpannableString
类名称:SpannableString

SpannableString介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);

代码示例来源:origin: nickbutcher/plaid

static SpannableStringBuilder linkifyPlainLinks(
    CharSequence input,
    ColorStateList linkTextColor,
    @ColorInt int linkHighlightColor) {
  final SpannableString plainLinks = new SpannableString(input); // copy of input
  // Linkify doesn't seem to work as expected on M+
  // TODO: figure out why
  //Linkify.addLinks(plainLinks, Linkify.WEB_URLS);
  final URLSpan[] urlSpans = plainLinks.getSpans(0, plainLinks.length(), URLSpan.class);
  // add any plain links to the output
  final SpannableStringBuilder ssb = new SpannableStringBuilder(input);
  for (URLSpan urlSpan : urlSpans) {
    ssb.removeSpan(urlSpan);
    ssb.setSpan(new TouchableUrlSpan(urlSpan.getURL(), linkTextColor, linkHighlightColor),
        plainLinks.getSpanStart(urlSpan),
        plainLinks.getSpanEnd(urlSpan),
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  }
  return ssb;
}

代码示例来源:origin: PhilJay/MPAndroidChart

private SpannableString generateCenterText() {
    SpannableString s = new SpannableString("Revenues\nQuarters 2015");
    s.setSpan(new RelativeSizeSpan(2f), 0, 8, 0);
    s.setSpan(new ForegroundColorSpan(Color.GRAY), 8, s.length(), 0);
    return s;
  }
}

代码示例来源:origin: stackoverflow.com

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

代码示例来源:origin: stackoverflow.com

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);

代码示例来源:origin: stackoverflow.com

TextView TV = (TextView)findViewById(R.id.mytextview01);
Spannable word = new SpannableString("Your message");        
word.setSpan(new ForegroundColorSpan(Color.BLUE), 0, word.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.setText(word);
Spannable wordTwo = new SpannableString("Your new message");        
wordTwo.setSpan(new ForegroundColorSpan(Color.RED), 0, wordTwo.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TV.append(wordTwo);

代码示例来源:origin: stackoverflow.com

public class TestActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
      TextView textView  = (TextView) findViewById(R.id.textview); 
      SpannableString ss = new SpannableString("abc"); 
      Drawable d = getResources().getDrawable(R.drawable.icon32); 
      d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight()); 
      ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BASELINE); 
      ss.setSpan(span, 0, 3, Spannable.SPAN_INCLUSIVE_EXCLUSIVE); 
      textView.setText(ss); 
}

代码示例来源:origin: stackoverflow.com

String s= "Hello Everyone";
SpannableString ss1=  new SpannableString(s);
ss1.setSpan(new RelativeSizeSpan(2f), 0,5, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 5, 0);// set color
TextView tv= (TextView) findViewById(R.id.textview);
tv.setText(ss1);

代码示例来源:origin: DaxiaK/MyDiary

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  this.getDialog().setCanceledOnTouchOutside(true);
  getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  View rootView = inflater.inflate(R.layout.dialog_fragment_no_entries, container);
  TV_no_entries_create = (TextView) rootView.findViewById(R.id.TV_no_entries_create);
  TV_no_entries_create.setOnClickListener(this);
  SpannableString content = new SpannableString(TV_no_entries_create.getText());
  content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
  TV_no_entries_create.setText(content);
  TV_no_entries_create.setTextColor(ThemeManager.getInstance().getThemeMainColor(getActivity()));
  return rootView;
}

代码示例来源:origin: stackoverflow.com

public static void clickify(TextView view, final String clickableText, 
  final ClickSpan.OnClickListener listener) {

  CharSequence text = view.getText();
  String string = text.toString();
  ClickSpan span = new ClickSpan(listener);

  int start = string.indexOf(clickableText);
  int end = start + clickableText.length();
  if (start == -1) return;

  if (text instanceof Spannable) {
    ((Spannable)text).setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
  } else {
    SpannableString s = SpannableString.valueOf(text);
    s.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    view.setText(s);
  }

  MovementMethod m = view.getMovementMethod();
  if ((m == null) || !(m instanceof LinkMovementMethod)) {
    view.setMovementMethod(LinkMovementMethod.getInstance());
  }
}

代码示例来源:origin: commonsguy/cw-omnibus

private void searchFor(String text) {
  TextView prose=(TextView)findViewById(R.id.prose);
  Spannable raw=new SpannableString(prose.getText());
  BackgroundColorSpan[] spans=raw.getSpans(0,
                       raw.length(),
                       BackgroundColorSpan.class);

  for (BackgroundColorSpan span : spans) {
   raw.removeSpan(span);
  }

  int index=TextUtils.indexOf(raw, text);

  while (index >= 0) {
   raw.setSpan(new BackgroundColorSpan(0xFF8B008B), index, index
     + text.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
   index=TextUtils.indexOf(raw, text, index + text.length());
  }

  prose.setText(raw);
 }
}

代码示例来源:origin: wangdan/AisenWeiBo

SpannableString value = SpannableString.valueOf(tv.getText());
    MyURLSpan[] urlSpans = value.getSpans(0, value.length(), MyURLSpan.class);
    int findStart = 0;
    int findEnd = 0;
    for (MyURLSpan urlSpan : urlSpans) {
      int start = value.getSpanStart(urlSpan);
      int end = value.getSpanEnd(urlSpan);
      if (start <= offset && offset <= end) {
        find = true;
      WebURLEmotionSpan[] webURLEmotionSpans = value.getSpans(0, value.length(), WebURLEmotionSpan.class);
      findStart = 0;
      findEnd = 0;
      for (WebURLEmotionSpan webURLEmotionSpan : webURLEmotionSpans) {
        int start = value.getSpanStart(webURLEmotionSpan);
        int end = value.getSpanEnd(webURLEmotionSpan);
        if (start <= offset && offset <= end) {
          find = true;
          value.removeSpan(webURLEmotionSpan);
          webURLEmotionSpan.setClickDown(true);
          value.setSpan(webURLEmotionSpan, start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
          tv.setText(value);
      value.setSpan(backgroundColorSpan, findStart, findEnd, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
  case MotionEvent.ACTION_CANCEL:
  case MotionEvent.ACTION_UP:

代码示例来源:origin: stackoverflow.com

TextView tv = (TextView) findViewById(R.id.textview);
SpannableString text = new SpannableString(myString);

text.setSpan(new TextAppearanceSpan(getContext(), R.style.myStyle), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new TextAppearanceSpan(getContext(), R.style.myNextStyle), 6, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

tv.setText(text, TextView.BufferType.SPANNABLE);

代码示例来源:origin: LuckSiege/PictureSelectorLight

public static void tempTextFont(TextView tv, int mimeType) {
  String text = tv.getText().toString().trim();
  String str = mimeType == PictureMimeType.ofAudio() ?
      tv.getContext().getString(R.string.picture_empty_audio_title)
      : tv.getContext().getString(R.string.picture_empty_title);
  String sumText = str + text;
  Spannable placeSpan = new SpannableString(sumText);
  placeSpan.setSpan(new RelativeSizeSpan(0.8f), str.length(), sumText.length(),
      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  tv.setText(placeSpan);
}

代码示例来源:origin: axxapy/apkExtractor

private void setAndHighlight(TextView view, String value, String pattern) {
    view.setText(value);
    if (pattern == null || pattern.isEmpty()) return;// nothing to highlight
    value = value.toLowerCase();
    for (int offset = 0, index = value.indexOf(pattern, offset); index >= 0 && offset < value.length(); index = value.indexOf(pattern, offset)) {
      Spannable span = new SpannableString(view.getText());
      span.setSpan(new ForegroundColorSpan(Color.BLUE), index, index + pattern.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
      view.setText(span);
      offset += index + pattern.length();
    }
  }
}

代码示例来源:origin: stackoverflow.com

TextView text=(TextView)findViewById(R.id.text);
   Spannable span = new SpannableString("Hi this is Android");
   span.setSpan(new RelativeSizeSpan(0.8f), 0  , 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
   text.setTextSize(20);
   text.setText(span);

代码示例来源:origin: stackoverflow.com

SpannableString text = new SpannableString("Lorem ipsum dolor sit amet");
 ClickableSpan clickableSpan = new ClickableSpan() {
  @Override
  public void onClick(View view) {
    startActivity(new Intent(context , NewActivity.class));
  }
};
text.setSpan(clickableSpan, start, end, 0); 
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(text);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setText(text, BufferType.SPANNABLE);

代码示例来源:origin: stackoverflow.com

SpannableString ss = new SpannableString("Android is a Software stack");
ClickableSpan clickableSpan = new ClickableSpan() {
  @Override
  public void onClick(View textView) {
    startActivity(new Intent(MyActivity.this, NextActivity.class));
  }
  @Override
  public void updateDrawState(TextPaint ds) {
      super.updateDrawState(ds);
      ds.setUnderlineText(false);
    }
};
ss.setSpan(clickableSpan, 22, 27, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

TextView textView = (TextView) findViewById(R.id.hello);
textView.setText(ss);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setHighlightColor(Color.TRANSPARENT);

代码示例来源:origin: stackoverflow.com

String text2 = text + CepVizyon.getPhoneCode() + "\n\n"
      + getText(R.string.currentversion) + CepVizyon.getLicenseText();

Spannable spannable = new SpannableString(text2);

spannable.setSpan(new ForegroundColorSpan(Color.WHITE), text.length(), (text + CepVizyon.getPhoneCode()).length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

myTextView.setText(spannable, TextView.BufferType.SPANNABLE);

代码示例来源:origin: stackoverflow.com

SpannableString s = new SpannableString("My Title");
s.setSpan(new TypefaceSpan(this, "MyTypeface.otf"), 0, s.length(),
    Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

// Update the action bar title with the TypefaceSpan instance
ActionBar actionBar = getActionBar();
actionBar.setTitle(s);

相关文章