How to make links in a TextView clickable?

后端 未结 30 3275

I have the following TextView defined:



        
相关标签:
30条回答
  • 2020-11-21 23:43

    Be sure to not use setAutoLinkMask(Linkify.ALL) when using setMovementMethod(LinkMovementMethod.getInstance()) and Html.fromHTML() on properly formatted HTML links (for example, <a href="http://www.google.com/">Google</a>).

    0 讨论(0)
  • 2020-11-21 23:43

    The reason you're having the problem is that it only tries to match "naked" addresses. things like "www.google.com" or "http://www.google.com".

    Running your text through Html.fromHtml() should do the trick. You have to do it programatically, but it works.

    0 讨论(0)
  • 2020-11-21 23:44

    Use below code:

    String html = "<a href=\"http://yourdomain.com\">Your Domain Name</a>"
    TextView textview = (TextView) findViewById(R.id.your_textview_id);
    textview.setMovementMethod(LinkMovementMethod.getInstance());
    textview.setText(Html.fromHtml(html));
    
    0 讨论(0)
  • 2020-11-21 23:45

    The above solutions didn't work for me, but the following did (and it seems a bit cleaner).
    First, in the string resource, define your tag opening chevrons using the HTML entity encoding, i.e.:

    &lt;a href="http://www.google.com">Google&lt;/a>
    

    and NOT:

    <a href="http://www.google.com">Google</a>
    

    In general, encode all the chevrons in the string like that. BTW, the link must start with http://

    Then (as suggested here) set this option on your TextView:

     android:linksClickable="true"
    

    Finally, in code, do:

    ((TextView) findViewById(R.id.your_text_view)).setMovementMethod(LinkMovementMethod.getInstance());
    ((TextView) findViewById(R.id.your_text_view)).setText(Html.fromHtml(getResources().getString(R.string.string_with_links)));
    

    That's it, no regexes or other manual hacks required.

    0 讨论(0)
  • 2020-11-21 23:45

    I added this line to the TextView: android:autoLink="web"
    Below is an example of usage in a layout file.

    layout.xml sample

        <TextView
            android:id="@+id/txtLostpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:autoLink="email"
            android:gravity="center"
            android:padding="20px"
            android:text="@string/lostpassword"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    
        <TextView
            android:id="@+id/txtDefaultpassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:autoLink="web"
            android:gravity="center"
            android:padding="20px"
            android:text="@string/defaultpassword"
            android:textAppearance="?android:attr/textAppearanceSmall" />
    

    string.xml

    <string name="lostpassword">If you lost your password please contact <a href="mailto:support@cleverfinger.com.au?Subject=Lost%20Password" target="_top">support@cleverfinger.com.au</a></string>
    
    <string name="defaultpassword">User Guide <a href="http://www.cleverfinger.com.au/user-guide/">http://www.cleverfinger.com.au/user-guide/</a></string>
    
    0 讨论(0)
  • 2020-11-21 23:49

    Richard, next time, you should add this code under TextView at the layout XML instead.

    android:autoLink="all"
    

    This should be like this.

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="@string/txtCredits"
        android:id="@+id/infoTxtCredits"
        android:autoLink="all"
        android:linksClickable="true">
    </TextView>
    

    You don't need to use this code (t2.setMovementMethod(LinkMovementMethod.getInstance());) in order to make the link clickable.

    Also, here's the truth: as long as you set the autoLink and the linksClickable, don't forget to add this at String.xml file so that the clickable link will work.

    <string name="txtCredits"><a href="http://www.google.com">Google</a></string>
    
    0 讨论(0)
提交回复
热议问题