Link Click event Interception in Android TextView

Source: Internet
Author: User

Layout file xml

1 <TextView2                 Android:layout_width= "Match_parent"3 Android:layout_height= "Match_parent"4 Android:id= "@+id/test_note"5 Android:autolink= "All"6                 />

Here autolink= "All" is a link to all types of links, including URLs, telephones, email addresses, etc.

In the Java code, the linked events are monitored.

1  Package Com.jayce.testlink; 2
3 ImportAndroid.net.Uri;4 ImportAndroid.os.Bundle;5 Importandroid.app.Activity;6 Importandroid.content.Intent;7 Importandroid.text.Spannable;8 ImportAndroid.text.SpannableStringBuilder;9 ImportAndroid.text.style.ClickableSpan;Ten ImportAndroid.text.style.URLSpan; One ImportAndroid.util.Log; A ImportAndroid.view.View; - ImportAndroid.view.View.OnClickListener; - ImportAndroid.widget.TextView; the
 -  Public class extends Activity {
 - @Override +      Public voidonCreate (Bundle savedinstancestate) { -         Super. OnCreate (savedinstancestate); + Setcontentview (r.layout.activity_text_link); ATextView TV =(TextView) Findviewbyid (r.id.test_note); atCharsequence Text =Tv.gettext (); -         /*
 - * Below are the contents of TextView (Here are 3 sample links and a normal string) - * 13888888888 - * www.google.com - * [email protected] in * Jaycetest -          */
 to         if(Textinstanceofspannable) {   +             intEnd =text.length (); -Spannable SP =(spannable) tv.gettext ();  theUrlspan[] spans = Sp.getspans (0, end, Urlspan.class);  *Spannablestringbuilder style =NewSpannablestringbuilder (text);  $Style.clearspans ();//should clear old spans
Panax Notoginseng              for(Urlspan span:spans) { -Jaycespan MySpan =NewJaycespan (Span.geturl ());  the Style.setspan (MySpan, Sp.getspanstart (span), sp.getspanend (span), spannable.span_exclusive_inclusive);  +             }   A Tv.settext (style); the         } +Tv.setonclicklistener (NewOnclicklistener () { -
 $ @Override $              Public voidOnClick (View v) { -                 //TODO auto-generated Method Stub -                 /*
< Span style= "Color:rgb (0, 128, 128);  line-height:1.5!important ">49   * Here is the listener for all click Messages  50   * can be marked off the link after the monitoring of non-link click  51   * For example, click the Jaycetest area  52  
   
    */ 
   
(+            )}); 
 Wu     }
< Span style= "Color:rgb (0, 128, 128); line-height:1.5!important ">56  private
     class  jaycespan extends   Clickablespan { 57  
< Span style= "Color:rgb (0, 128, 128); line-height:1.5!important ">58  private
     String Mspan;  59  
 -         Jaycespan (String span) {               mspan = span;    +         }  
 - @Override $          Public voidOnClick (View widget) { theLOG.E ("Jayce", "Span:" +Mspan); the             /*link is clicked the * Here you can do some of your own defined actions the              */
 -Intent Intent =NewIntent (intent.action_view); in Intent.setdata (Uri.parse (Mspan)); the startactivity (intent); the         }   About     }   the
 *


Source: http://www.cnblogs.com/jayceli/archive/2012/09/03/2669475.html

From for notes (Wiz)

Link Click event Interception in Android 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.