The following is a description of some of Spannablestring's special handling of text: fonts, colors, underscores, links, and click events.
First look at the effect:
Layout code is not affixed, very simple is 3 textview. Now look at the code implementation in Mainactivity:
PrivateTextView MTvContent1;PrivateTextView MTvContent2;PrivateTextView MTvContent3;PrivateString CONTENTSTR1 ="Click events, Underline, font color";PrivateString CONTENTSTR2 ="Baidu Link";PrivateString CONTENTSTR3 ="Bold";@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Set Click events, underline, font colorMTvContent1 = (TextView) Findviewbyid (R.ID.TV_CONTENT1); Spannablestring SS1 =NewSpannablestring (CONTENTSTR1); Ss1.setspan (NewClickablespan () {@Override Public void updatedrawstate(Textpaint DS) {Super. Updatedrawstate (DS); Ds.setunderlinetext (true);//Set underlineDs.setcolor (color.red);//Set Font color}@Override Public void OnClick(View widget) {Toast toast = Toast.maketext (mainactivity. This,"click", Toast.length_short); Toast.show (); } },0, Contentstr1.length (), spanned.span_exclusive_exclusive); Mtvcontent1.settext (SS1); Mtvcontent1.sethighlightcolor (color.transparent); Mtvcontent1.setmovementmethod (Linkmovementmethod.getinstance ());//Settings linkMtvcontent2= (TextView) Findviewbyid (R.id.tv_content2); Spannablestring SS2 =NewSpannablestring (CONTENTSTR2); Ss2.setspan (NewUrlspan ("Http://www.baidu.com"),0, Contentstr2.length (), spanned.span_exclusive_exclusive); Mtvcontent2.settext (SS2); Mtvcontent2.setmovementmethod (Linkmovementmethod.getinstance ());//Set Bold:mtvcontent3= (TextView) Findviewbyid (R.ID.TV_CONTENT3); Spannablestring SS3 =NewSpannablestring (CONTENTSTR3); Ss3.setspan (NewStylespan (Android.graphics.Typeface.BOLD),0, Contentstr3.length (), spanned.span_exclusive_exclusive); Mtvcontent3.settext (SS3); }}
This is the entire code: where the click and link events, pay special attention to the need to setmovementmethod, otherwise it may not respond to the event.
Android spannablestring Basic Usage