Function:
1. Display a picture in TextView, similar to a chat containing an emoticon image
2. Set the TextView section text to click
Description
Spannablestring implements the Charsequence interface, you can mark the specified part of text as a Characterstyle object, such as Imagespan,clickablespan, TextView a section of text marked as Characterstyle for specific processing when displayed
Code Listing 1: Displaying a picture in TextView
TextView
Mtextview = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Create a Spannablestring object
spannablestring Stringspan = new Spannablestring ("0123456789");
Create a Characterstyle object
Imagespan Imagespan = new Imagespan (this, R.DRAWABLE.P1);
Mark Spannablestring 0~3 A total of 3 characters as Imagespan object (Characterstyle)
Stringspan.setspan (Imagespan, 0, 3, spannable.span_exclusive_inclusive);
Mark the spannablestring of 1 characters as a Imagespan object
Note: A Imaggespan object can only be marked once
Stringspan.setspan (New Imagespan (THIS,R.DRAWABLE.P2), 4, 5, spannable.span_exclusive_inclusive);
Display text, at this time, the output is: Picture (p1) 3 pictures (p2) 56789
Mtextview.settext (Getspannablestring ());
Code Listing 2: displaying some clickable text in the TextView
TextView
Mtextview = (TextView) Findviewbyid (R.ID.TEXTVIEW1);
Create a Spannablestring object
spannablestring Stringspan = new Spannablestring ("0123456789");
Create a Clickablespan object to mark clickable text
Clickablespan Clickspan = new Clickablespan () {
callback method when text is clicked when tagged
@Override
public void OnClick (View arg0) {
LOG.I ("DTV", "click");
}
The style used to define the tagged text
@Override
public void Updatedrawstate (Textpaint ds) {
Super.updatedrawstate (DS);
Set No underline
Ds.setunderlinetext (FALSE);
}
};
Mark text
Stringspan.setspan (Clickspan, 6, 9, spannable.span_exclusive_exclusive);
Display text, at this time, the output is: Picture (p1) 3 pictures (p2) 56789
Mtextview.settext (Stringspan);
Mtextview.setmovementmethod (Linkmovementmethod.getinstance ());//must have, otherwise the OnClick method will not be called