如何在可点击的文本视图中设置超链接?安卓Java

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

How to set a hyperlink in clickable textview? Android Java

javaandroidurlhyperlinktextview

提问by KD-21

I have a textView with one URL.. But I don't want to show the whole URL but only a few words like: Click here. And when the textview is clicked.. The application need to open te URL "behind" the words Click here..

我有一个带有一个 URL 的 textView .. 但我不想显示整个 URL,而只想显示几个词,例如:单击此处。当文本视图被点击时.. 应用程序需要打开“后面”字样的 URL 点击这里..

For your information:The textView is clickable now. The URL displays correctly. When the URL is clicked, the browser will start, and load the URL correctly.

供您参考:textView 现在可点击。URL 正确显示。单击 URL 时,浏览器将启动,并正确加载 URL。

I only want to change the text of the link what is visible.

我只想更改可见的链接文本。

EDIT:Everytime the app restart or reload, the url can be different, so it is nog always the same URL.

编辑:每次应用程序重新启动或重新加载时,网址都可能不同,因此它始终是相同的网址。

回答by PKlumpp

Change the text in your textView to whatever you like. And provide the URL as a variable in your code

将 textView 中的文本更改为您喜欢的任何内容。并在代码中提供 URL 作为变量

EDIT: You say the URL changes: So just save the URL in a variable. Would be interesting how your app receives the new url

编辑:您说 URL 更改:因此只需将 URL 保存在变量中。您的应用如何接收新网址会很有趣

回答by riggaroo

Have you tried using html to achieve this? So for example have the following as your text and then setting

您是否尝试过使用 html 来实现这一目标?因此,例如将以下内容作为您的文本,然后设置

String text = "<a href='www.link.com'>Click here</a>";

textView.setText(Html.fromHtml(text));

回答by duggu

use below code :-

使用以下代码:-

    android:autoLink="web"

like this

像这样

<TextView
    android:id="@+id/txt_post_message"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="5dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:autoLink="web"
    android:text=""
    android:textColor="@color/wall_msg"
    android:textSize="16sp" />