How do I make links in a TextView clickable?

2018-12-31 03:18发布

I have the following TextView defined:

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

where @string/txtCredits is a string resource that contains <a href="some site">Link text</a>.

Android is highlighting the links in the TextView, but they do not respond to clicks. Can someone tell me what I'm doing wrong? Do I have to set an onClickListener for the TextView in my activity for something as simple as this?

Looks like it has to do with the way I define my string resource. This does not work:

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

But this does:

<string name="txtCredits">www.google.com</string>

Which is a bummer because I would much rather show a text link than show the full URL.

30条回答
呛了眼睛熬了心
2楼-- · 2018-12-31 04:05

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.

查看更多
ら面具成の殇う
3楼-- · 2018-12-31 04:07

Here is very one line android code to make phone and url selectable from textView no matter what is string and what is data. You dont need to use any HTML tags for this.

TextView textView = (TextView)findViewById(R.id.textView1);
textView.setText("some url is www.google.com phone 7504567890 another url lkgndflg.com ");

// Makes the textView's Phone and URL (hyperlink) select and go.
Linkify.addLinks(textView, Linkify.WEB_URLS | Linkify.PHONE_NUMBERS);
查看更多
人气声优
4楼-- · 2018-12-31 04:07

Don't know if it's worth adding another answer, but just in case...

I had to hunt this down in a couple places but finally got this version of the code to work.

strings.xml:

<string name="name1">&lt;a href="http://www.google.com">link text1&lt;/a></string>
<string name="name2">&lt;a href="http://www.google.com">link text2&lt;/a></string>

myactivity.xml:

<TextView 
    android:id="@+id/textview1"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginTop="5dp" />

<TextView 
    android:id="@+id/textview2"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:layout_marginTop="5dp" />

myactivty.java (in onCreate()):

TextView tv1 = (TextView)findViewById(R.id.textview1);
TextView tv2 = (TextView)findViewById(R.id.textview2);

tv1.setText(Html.fromHtml(getResources().getString(R.string.name1)));
tv2.setText(Html.fromHtml(getResources().getString(R.string.name2)));
tv1.setMovementMethod(LinkMovementMethod.getInstance());
tv2.setMovementMethod(LinkMovementMethod.getInstance());

This will create two clickable hyperlinks with the text link text1 and link text2 which redirect the user to google.

查看更多
低头抚发
5楼-- · 2018-12-31 04:08

i used this simply

Linkify.addLinks(TextView, Linkify.ALL);

makes the links clickable given here

查看更多
十年一品温如言
6楼-- · 2018-12-31 04:08

I use the autolink to "auto underline" the text, but just made an "onClick" that manages it. (I ran into this problem myself)

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:textSize="18dp"
            android:autoLink="all"
            android:text="@string/twitter"
            android:onClick="twitter"/>

public void twitter (View view)
    {
        try
        {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://twitter.com/onaclovtech"));
            startActivity(browserIntent);

        }
        finally
        {
        }
    }

Doesn't require any permissions, as you are passing the intent off to apps that manage those resources, (I.E. browser).

This was what worked for me. Good luck.

查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 04:10

If you want to add HTML-like link, all you need to do is:

  • add a resource HTML-like string:

     <string name="link"><a href="https://www.google.pl/">Google</a></string>
    
  • add your view to the layout with NO link-specific configuration at all:

     <TextView
        android:id="@+id/link"
        android:text="@string/link" />`
    
  • add appropriate MovementMethod programmatically to your TextView:

     mLink = (TextView) findViewById(R.id.link);
     if (mLink != null) {
       mLink.setMovementMethod(LinkMovementMethod.getInstance());
     }
    

That's it! And yes, having options like "autoLink" and "linksClickable" working on explicit links only (not wrapped into html tags) is very misleading to me too...

查看更多
登录 后发表回答