android中TextView内的自动链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10246366/
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 03:00:31  来源:igfitidea点击:

Autolink inside a TextView in android

androidtextviewautolink

提问by Rahul Baradia

How to give autolink for some part of textview ? For example : My text inside TextView is "Please click here to open this webpage". I want to show link for only the text "here". And I should open that webpage onclick of the text "here" but not on the anywhere of TextView.

如何为 textview 的某些部分提供自动链接?例如:我在 TextView 中的文字是“请单击此处打开此网页”。我只想显示“此处”文本的链接。我应该在点击文本“此处”时打开该网页,而不是在 TextView 的任何地方。

回答by asish

Put a String in string.xml

在 string.xml 中放入一个字符串

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="txtCredits">Support: <a href="http://www.stackoverflow.com">click here</a></string>
</resources>

And you can use it in textView like this:

你可以像这样在 textView 中使用它:

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

EDIT

编辑

For some reason above code does not work properly. So, add below code also,

由于某种原因,上面的代码不能正常工作。因此,还添加以下代码,

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

回答by Joakim Berglund

Textviews are capable of displaying HTML, which solves your problem. Wrap what you want clickable in a hyperlink:

Textviews 能够显示 HTML,这解决了您的问题。将您想要点击的内容包装在超链接中:

String html = "My link is <a href=\"http://google.com\">here</a>";
myTextView.setText(Html.fromHtml(html));

回答by Agam Shah

use simple Url in strings.xml :

在 strings.xml 中使用简单的 Url :

<string name="autolink_val">Please Click Here : http://www.google.com</string>

And in Java code write this:

在 Java 代码中这样写:

<TextView android:id="@+id/linkVal"   
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:autoLink="web" 
          android:text="@string/autolink_val1"/>`

回答by Saleem Kalro

You can test it with following code:

您可以使用以下代码对其进行测试:

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="http://www.yahoo.com"
    android:autoLink="web"
    />

回答by vtuhtan

Use HTML syntax in strings.xml:

在 strings.xml 中使用 HTML 语法:

<string name="test">Click &lt;a href="http://vtuhtan.info"&gt;here&lt;/a&gt;</string>

Set TextView properties to have links clickable and auto link.

将 TextView 属性设置为可点击链接和自动链接。

TextView tv = findViewById(R.id.textView);
tv.setText(Html.fromHtml(getResources().getString(R.string.test)));