Advanced usage of Android TextView

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.