Android 如何使 TextView 中的链接可点击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2734270/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:08:46  来源:igfitidea点击:

How to make links in a TextView clickable?

androidhyperlinktextviewclickable

提问by Richard

I have the following TextView defined:

我定义了以下 TextView:

<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/txtCreditsis a string resource that contains <a href="some site">Link text</a>.

其中@string/txtCredits是包含<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?

Android 正在突出显示 TextView 中的链接,但它们不响应点击。有人能告诉我我做错了什么吗?对于像这样简单的事情,我是否必须在我的活动中为 TextView 设置一个 onClickListener?

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.

这是一个无赖,因为我宁愿显示一个文本链接而不是显示完整的 URL。

回答by Richard

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

在 API 演示中,我找到了问题的解决方案:

Link.java:

链接.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 上的大部分属性以匹配演示中的内容。

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

重要提示autoLink="web"如果您正在调用 ,请不要忘记删除setMovementMethod()

回答by Janusz

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

我只使用android:autoLink="web"它,它工作正常。单击该链接可打开浏览器并显示正确的页面。

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.

我能猜到的一件事是链接上方有其他一些视图。透明的东西会填充整个父级,但不会在链接上方显示任何内容。在这种情况下,点击会转到此视图而不是链接。

回答by Jeshurun

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:
  • android:autoLink="web"如果您的 HTML 中有完整链接,则有效。以下内容将以蓝色突出显示并可点击:
  • Some text <a href="http://www.google.com">http://www.google.com</a>
  • Some text http://www.google.com
  • 一些文字 <a href="http://www.google.com">http://www.google.com</a>
  • 一些文字 http://www.google.com
  • view.setMovementMethod(LinkMovementMethod.getInstance());will work with the following (will be highlighted and clickable):
  • view.setMovementMethod(LinkMovementMethod.getInstance());将使用以下内容(将突出显示并可点击):
  • 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>
  • 一些文字 <a href="http://www.google.com">http://www.google.com</a>
  • 一些文字 http://www.google.com
  • 一些文字 <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 NOTwork with such links.

请注意,第三个选项有一个超链接,但链接的描述(标签之间的部分)本身不是链接。android:autoLink="web"确实不是这样的联系工作。

  • 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).
  • android:autoLink="web"如果在 XML 中设置将覆盖view.setMovementMethod(LinkMovementMethod.getInstance());(即;第三类链接将突出显示,但不可点击)。

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 alllinks to be clickable.

这个故事的寓意是view.setMovementMethod(LinkMovementMethod.getInstance());在您的代码中使用,android:autoLink="web"如果您希望所有链接都可点击,请确保您的 XML 布局中没有。

回答by B?a?ej Czapp

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

上述解决方案对我不起作用,但以下解决方案有效(而且看起来更干净)。
首先,在字符串资源中,使用 HTML 实体编码定义您的标签开口 V 形,即:

&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://

一般来说,像这样对字符串中的所有 V 形符号进行编码。顺便说一句,链接必须以http://

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

然后(如建议在这里)设置你的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.

就是这样,不需要正则表达式或其他手动黑客。

回答by jai_b

i used this simply

我简单地使用了这个

Linkify.addLinks(TextView, Linkify.ALL);

makes the links clickable given here

使此处给出的链接可点击

回答by vizZ

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

如果你想添加类似 HTML 的链接,你需要做的就是:

  • 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());
     }
    
  • 添加一个类似于 HTML 的资源字符串:

     <string name="link"><a href="https://www.google.pl/">Google</a></string>
    
  • 将您的视图添加到布局中,根本没有链接特定的配置:

     <TextView
        android:id="@+id/link"
        android:text="@string/link" />`
    
  • 以编程方式将适当的 MovementMethod 添加到您的 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...

就是这样!是的,像“autoLink”和“linksClickable”这样的选项只在显式链接上工作(不包含在 html 标签中)对我来说也很误导......

回答by Shanewaj

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

我将此行添加到TextViewandroid:autoLink="web"
下面是布局文件中的使用示例。

layout.xmlsample

layout.xml样本

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

<string name="lostpassword">If you lost your password please contact <a href="mailto:[email protected]?Subject=Lost%20Password" target="_top">[email protected]</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>

回答by Asesha George

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

以下内容适用于在 Android 应用程序中寻找文本和超链接组合的任何人。

In string.xml:

string.xml

<string name="applink">Looking for Digital Visiting card? 
<a href="https://play.google.com/store/apps/details?id=com.themarkwebs.govcard">Get it here</a>
</string>

Now you can utilise this stringin any given Viewlike this:

现在你可以string在任何给定的情况下使用View它:

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

现在,在您的 Activity 或 Fragment 中,执行以下操作:

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.

到目前为止,您不需要设置android:autoLink="web"android:linksClickable="true"使用这种方法。

I hope you will find this helpful.

我希望你会发现这很有帮助。

回答by Bebin T.N

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());

回答by Ahmed Mostafa

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

只有你需要在 xml 的文本视图中添加这个

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