Textview uses spannablestring to set compound text spannablestring to achieve the link effect of textview
1、 Introduction
Textview uses spannablestring to set compound text. Textview is usually used to display ordinary text, but sometimes it is necessary to set the style and events of some of the text. The Android system processes the specified text through the spannablestring class, which has the following functions:
1. Backgroundcolorspan background color 2. Clickablespan text can be clicked, with click events 3. Foregroundcolorspan text color (foreground color) 4. Maskfilterspan decoration effects, such as blurmaskfilter and embossmaskfilter 5. Metricaffectingspan parent class, which is generally not used 6. Rasterizerspan raster effect 7. Strikethrough span delete line (middle dash) 8. Suggestionspan is equivalent to placeholder 9, underlinespan underline 10, absolutesizespan absolute size (text font) 11 and dynamicdrawablespan to set the picture based on the text baseline or bottom alignment. 12. Imagespan picture 13. Relativesizespan relative size (text font) 14. Replacementspan parent class, generally not used 15. Scalexspan scales based on X-axis 16. Stylespan font styles: bold, italic, etc. 17. Subscriptspan subscript (mathematical formula will be used) 18. Superscript (mathematical formula will be used) 19. Textappearancespan text appearance (including font, size, style and color) 20, typefacespan text font 21, urlspan text hyperlink
Class architecture of spannablestring (part)
2、 Method
1) Use spannablestring to set the link click event method for the textview object
Step 1: set the string content
String text = "click me to open activity";
Step 2: add various attributes to the string content. Here is the clickablespan attribute with spannablestring
SpannableString spannableString=new SpannableString(text);
Spannablestring. Setspan (New clickablespan() {omit onclic method}, 3, text. Length(), spanned. Span_exclusive_exclusive);
Step 3: add the text content set above in textview
tv_ spannableString.setText(spannableString);
Step 4: set the mouse click response event
This step is required in the HTML method. At present, AutoLink is not specified. I feel it is also necessary. There is no compilation error without this sentence
Spannablestring. Setspan (New clickablespan() {omit onclic method} in the middle,
tv_ spannableString.setMovementMethod(LinkMovementMethod.getInstance());
2) Spannablestring is a method for setting various properties for a control
1. Convert string type to spannablestring type object
SpannableString spannableString=new SpannableString(text);
2. Execute the setspan method on the spannablestring object
Spannablestring. Setspan (New clickablespan() {omit onclic method},}, spanned. Span_exclusive_exclusive);
Note: clickablespan () is used to set click events. If you want to set other effects, use other effects,
Just as many as listed above
3、 Code example
design sketch
Appear after clicking
code:
fry.ActivityDemo3
/textViewDemo1/res/layout/activity03.xml
The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.