We said it before. We want to implement in the TextView component, can display the URL, email and other special information , this information click can achieve jump, the real meaning of hyperlinks
This class is spannablestring to achieve the above requirements.
Because spannable and so on finally realized the Charsequence interface , so can direct spannablestring and spannablestringbuilder through textview.settext ( ) is set to TextView.
spannablestring: The special is that there is a setspan () function that can add various formats or styles (spans) to these stored strings, displaying the original string in a different style, For example, in the original string underline, hyperlink jump, add background color, change the font color, use the picture to replace the specified text, and so on.
void Setspan (Object what, int start, int end, int flags)
function meaning: Set the span style for spannablestring or spannablestringbuilder a specific range of strings, you can set multiple (for example, underline and strikethrough), The FALG parameter identifies whether the same style is applied to the newly inserted character when the new character is inserted before and after the marked range. (This will be explained in detail later in this example)
Parameter description:
Object What: corresponds to the various spans, which will be mentioned later;
int start: Start applying the location of the specified span, starting with index 0
int end: Ends the position where the specified span is applied, and the effect does not include this position. For example, if the number is 3 (that is, the 4th character), the 4th character will have no effect. It can be seen from the example below.
INT flags: There are four of the following values
Spannable.span_exclusive_exclusive: None before and after , that is, inserting new characters before and after a specified range does not apply a new style
Spannable.span_exclusive_inclusive: not included before , included later . That is, the new style is applied only when a new character is inserted after the range character
Spannable.span_inclusive_exclusive: included earlier , not included later .
Spannable.span_inclusive_inclusive: both front and back are included .
1. First we implement the jump, here is the implementation of the jump implementation activity, the core code is as follows:
String Text = "Show Activity1"; Spannablestring spannablestring=Newspannablestring (text); Spannablestring.setspan (NewClickablespan () {@Override Public voidOnClick (View widget) {Intent Intent=NewIntent (mainactivity. This, Activity1.class); StartActivity (Intent); }},0, Text.length (), spanned.span_exclusive_exclusive);//set Spannablestring object settings two TextView component contentsTextview.settext (spannablestring);//When you click a link, you must set the Movementmethod object for whatever action you want to perform.Textview.setmovementmethod (Linkmovementmethod.getinstance ());
2. Font style settings
In the previous small example, you should also be able to see that there are three steps to apply a span in total:
1. Construct string
2. Construct span
3. Use Setspan () to apply this span to a specified range of strings
(1) Font Color setting (Foregroundcolorspan)
New Spannablestring ("Welcome to Harvic's blog"); // and construct a span that changes the color of the font. New Foregroundcolorspan (color.blue); // apply this span to the specified range of font Spanstring.setspan (span, 1, 5, spannable.span_exclusive_inclusive); // set to EditText show out edittext.settext (spanstring);
(2) Font background color (backgroundcolorspan)
New spannablestring ("Welcome to Harvic's blog"); New Backgroundcolorspan (color.yellow); 0, 3, spannable.span_exclusive_exclusive);
(3) Font size (Absolutesizespan)
New spannablestring ("Welcome to Harvic's blog"); New Absolutesizespan (+); 2, 5, spannable.span_inclusive_inclusive); Edittext.settext (spanstring);
(4) bold, italic (Stylespan)
New spannablestring ("Welcome to Harvic's blog"); New Stylespan (typeface.bold_italic); 1, 4, spannable.span_exclusive_exclusive); Edittext.settext (spanstring);
(5) Strikethrough (Strikethroughspan)
New spannablestring ("Welcome to Harvic's blog"); New Strikethroughspan (); 2, 5, spannable.span_exclusive_exclusive); Edittext.settext (spanstring);
(6) Underline (Underlinespan)
New spannablestring ("Welcome to Harvic's blog"); New Underlinespan (); 1, 4, spannable.span_exclusive_exclusive); Edittext.settext (spanstring);
(7) Picture replacement (Imagespan)
Imagspan has a lot of constructors, which are usually constructed by passing in the Drawableg, and the detailed construction instructions look here: http://developer.android.com/reference/android/text/style/ImageSpan.html
New spannablestring ("Welcome to Harvic's blog"); = getresources (). getdrawable (R.drawable.ic_launcher); D.setbounds (0, 0, D.getintrinsicwidth (), D.getintrinsicheight ()); New Imagespan (d, Imagespan.align_baseline); 2, 4, spannable.span_exclusive_exclusive); Edittext.settext (spanstring);
The difference between this function is that the first few are based on the original text with special effects, and here is the use of images to replace the text. If you encounter a function that does not support displaying a picture, such as a canvas drawing in the previous article. Will degenerate into a string, which is displayed in the original string string.
Use of the Android (Java) Learning Note 140:spannablestring class