SoFunction
Updated on 2025-04-10

Examples of three common ways to implement link-linked text event monitoring in Android TextView

This article describes three common ways of Android TextView to implement link-linked text event monitoring. Share it for your reference, as follows:

/**
  * TextView implements text link jump function
  * @description:
  * @author ldm
  * @date 2016-4-21 4:34:05 pm
  */
public class TextViewLinkAct extends Activity {
  private TextView tv_3;
  private TextView tv_4;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    (savedInstanceState);
    setContentView();
    setTextViewLink();
  }
  /**
    * Implement the function of clicking and jumping in Chinese textView through different ways
    *
    * @description:
    * @author ldm
    * @date 2016-4-21 4:24:13 pm
    */
  private void setTextViewLink() {
    // Implement jump in Html format href link    tv_3 = (TextView) findViewById(.text3);
    tv_3.setText(Html
        .fromHtml("<b>text3: Constructed from HTML programmatically.</b> Text with a "
            + "<a href=\"\">link</a> "
            + "created in the Java source code using HTML."));
    tv_3.setMovementMethod(());
    // Implement link effect through the setMovementMethod method of SpannableString    SpannableString ss = new SpannableString(
        "text4: Manually created spans. Click here to dial the phone.");
    (new StyleSpan(), 0, 30,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    (new URLSpan("tel:4155551212"), 31 + 6, 31 + 10,
        Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tv_4 = (TextView) findViewById(.text4);
    tv_4.setText(ss);
    tv_4.setMovementMethod(());
  }
}

Layout file

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:andro
  android:layout_width="match_parent"
  android:layout_height="wrap_content" >
  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:divider="?android:attr/listDivider"
    android:orientation="vertical"
    android:showDividers="middle" >
    <!-- ByautoLinkProperties settingsTextViewLink function. -->
    <TextView
      android:
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:autoLink="all"
      android:paddingBottom="8dp"
      android:text="<b>text1: Various kinds
   of data that will be auto-linked.</b> In
   this text are some things that are actionable. For instance,
   you can click on  and it will launch the
   web browser. You can click on  too. If you
   click on (415) 555-1212 it should dial the phone. Or just write
   foobar@ for an e-mail link. If you have a URI like
   /lala/foobar@ you should get the
   full link not the e-mail address. Or you can put a location
   like 1600 Amphitheatre Parkway, Mountain View, CA 94043. To summarize:
   , or 650-253-0000, somebody@,
   or 9606 North MoPac Expressway, Suite 400, Austin, TX 78759."
      android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
      android:
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingBottom="8dp"
      android:paddingTop="8dp"
      android:textAppearance="?android:attr/textAppearanceMedium" />
    <TextView
      android:
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:paddingTop="8dp"
      android:textAppearance="?android:attr/textAppearanceMedium" />
  </LinearLayout>
</ScrollView>

Among them, the selected items of the Android:autoLink attribute in the code are: none (no link effect), web (web link), email (email), phone (call), map (localization) and all (default automatically link).

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android debugging skills and solutions to common problems》、《Summary of the usage of basic Android components》、《Android View View Tips Summary》、《Android layout layout tips summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.