Android implements partial text listener events in TextView

Source: Internet
Author: User

Usually we only need to use TextView to display the text information, display to the user to see, and click the corresponding listener event to the button and other controls to deal with, of course, TextView can also implement the Click event. Sometimes we have to click on some of the text in TextView, what should we do? There are generally the following three ways:

1, set the Android:autolink property of TextView in the layout file, such as setting android:autolink= "Phone", you can make a direct call by clicking on the phone number. Android:autolink Sets whether text is displayed as a clickable link when the text is a URL link/email/phone number/map. Optional values (None/web/email/phone/map/all), and so on. This is the simplest, but the goal to achieve is relatively simple, such as only can call or send e-mail and so on.

2, use the HREF chain in HTML format, for example:

String content= "<font color= #ff0000 > click on the corresponding text to jump to the Web page" + "</font><a href= ' http://www.baidu.com ' > Baidu </a> "; TextView linktextview= (TextView) Findviewbyid (R.id.linktextview);Textview.settext (html.fromhtml (content));

Click on the above Baidu two words can jump directly to Baidu Web page. This way generally can only jump to the Web page.

3, when we want to achieve the click of the text to jump to the specified page or achieve a specific effect (such as pop-up dialog box, toast information, etc.) we can use spannablestring to achieve:

TextView textview= (TextView) Findviewbyid (r.id.tv);

spannablestring spanstr = new Spannablestring (Textview.gettext (). toString ()); Nounderlinespan Mnounderlinespan = new Nounderlinespan ();//The purpose of this sentence is to remove the special text underline effect Spanstr.setspan (New Foregroundcolorspan (Color.parsecolor ("#00b5f0")), S.length ()-7, S.length (), spannable.span_exclusive_inclusive);//Set some text text color, here the text length must be greater than 7 .S.setspan (New Clickablespan () {
@Overridepublic void OnClick (View widget) { //This is the click event to achieve your desired effect }}, Spanstr.length ()-7, S.length (), Spanned.span_mark_mark); Spanstr.setspan (Mnounderlinespan, Spanstr.length ()-7, Spanstr.length (), Spannable.span_exclusive_ INCLUSIVE);Textview.settext (SPANSTR); Textview.setmovementmethod (Linkmovementmethod.getinstance ());

To implement the clear text underline:

Public class Nounderlinespan extends Underlinespan {
@Overridepublic void Updatedrawstate (Textpaint ds) { Ds.setcolor (ds.linkcolor); Ds.setunderlinetext (false); }}

Of course there are other ways!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android implements partial text listener events in TextView

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.