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 03:52

by using linkify: Linkify take a piece of text and a regular expression and turns all of the regex matches in the text into clickable links

TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("http://www.domain.com");
Linkify.addLinks(textView, Linkify.WEB_URLS);

Don't forget to

import android.widget.TextView;
查看更多
不流泪的眼
3楼-- · 2018-12-31 03:52

This is how I solved clickable and Visible links in a TextView (by code)

private void setAsLink(TextView view, String url){
        Pattern pattern = Pattern.compile(url);
        Linkify.addLinks(view, pattern, "http://");
        view.setText(Html.fromHtml("<a href='http://"+url+"'>http://"+url+"</a>"));
    }
查看更多
还给你的自由
4楼-- · 2018-12-31 03:54

Just wasted so much time to figure out you have to use getText(R.string.whatever) instead of getString(R.string.whatever)...

Anyways, here is how I got mine working. With multiple hyperlinks in the same text view too.

    TextView termsTextView = (TextView) getActivity().findViewById(R.id.termsTextView);
    termsTextView.append("By registering your account, you agree to our ");
    termsTextView.append(getText(R.string.terms_of_service));
    termsTextView.append(", ");
    termsTextView.append(getText(R.string.fees));
    termsTextView.append(", and the ");
    termsTextView.append(getText(R.string.stripe_connected_account_agreement));

    termsTextView.setMovementMethod(LinkMovementMethod.getInstance());



            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/termsTextView"/>

string example

    <string name="stripe_connected_account_agreement"><a href="https://stripe.com/connect/account-terms">Stripe Connected Account Agreement</a></string>
查看更多
刘海飞了
5楼-- · 2018-12-31 03:55

If using XML based TextView, for your requirement you need to do just two things:

  1. Identify your link in the string, such as "this is my WebPage." You can add it in xml or in the code.

  2. In the xml that has the TextView, add these:


android:linksClickable="true"

android:autoLink="web"
查看更多
柔情千种
6楼-- · 2018-12-31 03:58

Autolink phone does not worked for me. The following worked like a charm,

TextView tv = (TextView) findViewById(R.id.emergencynos);
String html2="<br><br>Fire - <b><a href=tel:997>997</a> </b></br></br>";        
tv.append(Html.fromHtml(html2));
tv.setMovementMethod(LinkMovementMethod.getInstance());
查看更多
孤独总比滥情好
7楼-- · 2018-12-31 03:59

After spending some time with this, I have found that:

  • android:autoLink="web" works if you have full links in your HTML. The following will be highlighted in blue and clickable:
  • Some text <a href="http://www.google.com">http://www.google.com</a>
  • Some text http://www.google.com
  • view.setMovementMethod(LinkMovementMethod.getInstance()); will work with the following (will be highlighted and clickable):
  • Some text <a href="http://www.google.com">http://www.google.com</a>
  • Some text http://www.google.com
  • Some text <a href="http://www.google.com">Go to Google</a>

Note that the third option has a hyperlink, but the description of the link (the part between the tags) itself is not a link. android:autoLink="web" does NOT work with such links.

  • android:autoLink="web" if set in XML will override view.setMovementMethod(LinkMovementMethod.getInstance()); (i.e.; links of the third kind will be highlighted, but not clickable).

The moral of the story is use view.setMovementMethod(LinkMovementMethod.getInstance()); in your code and make sure you don't have android:autoLink="web" in your XML layout if you want all links to be clickable.

查看更多
登录 后发表回答