TextView is used to display the text, sometimes need to textview in the individual words set as hyperlinks, or set the color of individual words, fonts, etc., it is necessary to use the Spannable object, you can use the Spannable object to achieve the above settings.
:
Activity code:
Importandroid.app.Activity; ImportAndroid.graphics.Color; ImportAndroid.os.Bundle; Importandroid.text.Spannable; Importandroid.text.SpannableString; Importandroid.text.Spanned; ImportAndroid.text.method.LinkMovementMethod; ImportAndroid.text.style.BackgroundColorSpan; ImportAndroid.text.style.ForegroundColorSpan; ImportAndroid.text.style.StyleSpan; ImportAndroid.text.style.URLSpan; ImportAndroid.widget.TextView; Public classTextviewlinkactivityextendsActivity {TextView mytextview; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.main); Mytextview= (TextView) This. Findviewbyid (R.id.mytextview); //Create a Spannablestring objectSpannablestring SP =NewSpannablestring ("This sentence has Baidu hyperlinks, there are highlighting, so, or so, there are italic."); //Set up HyperlinksSp.setspan (NewUrlspan ("http://www.baidu.com"), 5, 7, spanned.span_exclusive_exclusive); //Set highlight style oneSp.setspan (NewBackgroundcolorspan (color.red), 17, 19, spannable.span_exclusive_exclusive); //Set highlight style twoSp.setspan (NewForegroundcolorspan (Color.yellow), 20,24, spannable.span_exclusive_inclusive); //Set ItalicSp.setspan (NewStylespan (Android.graphics.Typeface.BOLD_ITALIC), 27, 29, spannable.span_exclusive_inclusive); //spannablestring Object set to TextViewMytextview.settext (SP); //set TextView to clickMytextview.setmovementmethod (Linkmovementmethod.getinstance ()); } }
Text in Android textview hyperlink, color, font