csdnceshi56
lrony*
2010-04-29 01:41
采纳率: 0%
浏览 653

我如何使链接在一个 TextView 点击?

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.

转载于:https://stackoverflow.com/questions/2734270/how-do-i-make-links-in-a-textview-clickable

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

30条回答 默认 最新

  • csdnceshi73
    喵-见缝插针 2010-04-30 18:18
    已采纳

    Buried in the API demos I found the solution to my problem:

    Link.java:

        // text2 has links specified by putting <a> tags in the string
        // resource.  By default these links will appear but not
        // respond to user input.  To make them active, you need to
        // call setMovementMethod() on the TextView object.
    
        TextView t2 = (TextView) findViewById(R.id.text2);
        t2.setMovementMethod(LinkMovementMethod.getInstance());
    

    I removed most of the attributes on my TextView to match what was in the demo.

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/txtCredits"/>
    

    That solved it. Pretty difficult to uncover and fix.

    Important: Don't forget to remove autoLink="web" if you are calling setMovementMethod().

    点赞 评论
  • csdnceshi80
    胖鸭 2010-04-29 07:13

    I'm using only android:autoLink="web" and it works fine. A click on the link opens the browser and shows the correct page.

    One thing I could guess is that some other view is above the link. Something that is transparent fills the whole parent but don't displays anything above the link. In this case the click goes to this view instead of the link.

    点赞 评论
  • csdnceshi57
    perhaps? 2010-04-30 01:57

    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.

    点赞 评论
  • csdnceshi56
    lrony* 2012-01-24 18:56

    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>"));
        }
    
    点赞 评论
  • csdnceshi53
    Lotus@ 2012-03-21 22:05

    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>).

    点赞 评论
  • csdnceshi75
    衫裤跑路 2012-04-12 13:58

    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.

    点赞 评论
  • csdnceshi64
    游.程 2012-05-08 11:00

    Only what do you need to add this in text view in xml

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:autoLink="web"/>
    
    点赞 评论
  • csdnceshi55
    ~Onlooker 2012-06-29 10: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...

    点赞 评论
  • csdnceshi56
    lrony* 2012-11-14 10:41

    I noticed that using android:autoLink="web" thus

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:autoLink="web"/>
    

    worked OK for URLs but since I had an e-mail address and phone number that I wanted to link as well, I ended up using this line android:autoLink="all" like this

    <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content" 
            android:autoLink="all"/>
    

    and it worked like a charm.

    点赞 评论
  • csdnceshi54
    hurriedly% 2012-11-22 09:42

    Use this...

    TextView.setOnClickListener(new OnClickListener() {
    
                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Intent in=new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.twitter.com/"));
                            startActivity(in);
                        }
    
                    });
    

    and add permission in manifest file

    <uses-permission android:name="android.permission.INTERNET"/>
    
    点赞 评论
  • csdnceshi76
    斗士狗 2013-02-16 23:25

    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.

    点赞 评论
  • csdnceshi66

    You need only this:

    android:autoLink="web"
    

    Insert this line to TextView, that can be clickable with reference to the web. URL address set as a text of this TextView.

    Example:

     <TextView
        android:id="@+id/textViewWikiURL"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textStyle="bold"
        android:text="http://www.wikipedia.org/"
        android:autoLink="web" />
    
    点赞 评论
  • csdnceshi63
    elliott.david 2013-04-08 05:08

    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());
    
    点赞 评论
  • csdnceshi54
    hurriedly% 2013-04-11 23:36

    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"
    
    点赞 评论
  • csdnceshi55
    ~Onlooker 2013-04-25 03:20

    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>
    
    点赞 评论
  • weixin_41568196
    撒拉嘿哟木头 2013-06-18 06:14

    I hope this will help you;

    String value = "<html>Visit my blog <a href=\"http://www.maxartists.com\">mysite</a> View <a href=\"sherif-activity://myactivity?author=sherif&nick=king\">myactivity</a> callback</html>";
        TextView text = (TextView) findViewById(R.id.text);
    
    
        text.setText(Html.fromHtml(value));
        text.setMovementMethod(LinkMovementMethod.getInstance());
    
    点赞 评论
  • csdnceshi67
    bug^君 2013-12-17 23:45

    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.

    点赞 评论
  • weixin_41568126
    乱世@小熊 2014-02-25 02:20

    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>
    
    点赞 评论
  • csdnceshi74
    7*4 2014-03-26 13:00

    i used this simply

    Linkify.addLinks(TextView, Linkify.ALL);
    

    makes the links clickable given here

    点赞 评论
  • weixin_41568196
    撒拉嘿哟木头 2014-08-19 10:03

    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));
    
    点赞 评论
  • csdnceshi52
    妄徒之命 2015-02-07 20:15

    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;
    
    点赞 评论
  • csdnceshi73
    喵-见缝插针 2015-02-17 11:21

    Add CDATA to your string resource

    Strings.xml

    <string name="txtCredits"><![CDATA[<a href=\"http://www.google.com\">Google</a>]]></string>
    
    点赞 评论
  • csdnceshi67
    bug^君 2015-07-10 12:33

    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.

    点赞 评论
  • weixin_41568208
    北城已荒凉 2015-08-12 20:01

    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>
    
    点赞 评论
  • csdnceshi51
    旧行李 2015-11-09 19:31

    My code was like this:

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/link"
        android:text="@string/forgot"
        android:layout_marginTop="16dp"
        android:gravity="center"
        android:linksClickable="true"/>
    

    My Java code was like this:

    /*TextView action*/
            TextView textView = (TextView) findViewById(R.id.link);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
            textView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    startActivity(new Intent(LoginActivity.this,forgot.class));
                }
            });  
    

    This just points the link to another activity. But that link is clickable and works smoothly. Tested in Android Studio 1.5 (Preview)

    点赞 评论
  • weixin_41568184
    叼花硬汉 2015-12-01 10:28

    The accepted answer is correct, BUT it will mean that phone numbers, maps, email addresses, and regular links e.g. http://google.com without href tags will NO LONGER be clickable since you can't have autolink in the xml.

    The only complete solution to have EVERYTHING clickable that I have found is the following:

    Spanned text = Html.fromHtml(myString);
    URLSpan[] currentSpans = text.getSpans(0, text.length(), URLSpan.class);
    SpannableString buffer = new SpannableString(text);
    Linkify.addLinks(buffer, Linkify.ALL);
    for (URLSpan span : currentSpans) {
        int end = text.getSpanEnd(span);
        int start = text.getSpanStart(span);
        buffer.setSpan(span, start, end, 0);
    }
    textView.setText(buffer);
    textView.setMovementMethod(LinkMovementMethod.getInstance());
    

    And the TextView should NOT have android:autolink. There's no need for android:linksClickable="true" either; it's true by default.

    点赞 评论
  • csdnceshi79
    python小菜 2016-05-28 13:27

    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);
    
    点赞 评论
  • weixin_41568184
    叼花硬汉 2016-07-29 14:05

    The easiest thing that worked for me is to use Linkify

    TextView txt_Message = (TextView) view.findViewById(R.id.txt_message);
    txt_Message.setText("This is link https://www.google.co.in/");
    Linkify.addLinks(txt_Message, Linkify.WEB_URLS);
    

    and it will automatically detect the web urls from the text in the textview.

    点赞 评论
  • weixin_41568183
    零零乙 2017-07-28 08:26

    The following should work for anyone who is looking for a combination of text and hyperlink within an Android app.

    In string.xml:

    <string name="applink">Looking for the regular Zesteve App? 
    <a href="https://play.google.com/store/apps/details?id=zesteve.com.myapplication">Get it here</a>
    </string>
    

    Now you can utilise this string in any given View like this:

    <TextView
        android:id="@+id/getapp"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center"
        android:textColor="@color/main_color_grey_600"
        android:textSize="15sp"
        android:text="@string/applink"/>
    

    Now, in your Activity or Fragment, do the following:

    TextView getapp =(TextView) findViewById(R.id.getapp);
    getapp.setMovementMethod(LinkMovementMethod.getInstance());
    

    By now, you don't require to set android:autoLink="web" or android:linksClickable="true" using this approach.

    I hope you will find this helpful.

    点赞 评论
  • csdnceshi63
    elliott.david 2018-04-24 20:20

    [Tested in Pre-lollipop as well as in Lollipop and above]

    You can get your HTML string from the backend or from your resources files. If you put your text as an resource string, make sure to add the CDATA tag:

    <string name="your_text">![CDATA[...<a href="your_link">Link Title</a>  ...]]</string>
    

    Then in code you need to get the string and assign it as HTML and set a link movement method:

    String yourText = getString(R.string.your_text);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
       textView.setText(Html.fromHtml(yourText, Html.FROM_HTML_MODE_COMPACT));
    } else {
       textView.setText(Html.fromHtml(yourText));
    }
    
    try {
       subtext.setMovementMethod(LinkMovementMethod.getInstance());
    } catch (Exception e) {
       //This code seems to crash in some Samsung devices.
       //You can handle this edge case base on your needs.
    }
    
    点赞 评论

相关推荐