如何在Android中使用TextView实现可点击的URL链接,而不使用Java。

6

我在Textview上使用了可点击的URL,它可以正常工作。但是如何设置可点击和高亮的文本,并打开一个链接到浏览器中呢?通过Android XML或kotlin实现是可能的,而不需要使用像setText(Html.fromHtml(""))这样的Java代码。

String value = "<html>Visit Web <a href=\"http://www.domainname.com\">mysite</a> View</html>";
    TextView text = (TextView) findViewById(R.id.text);
    text.setText(Html.fromHtml(value));
    text.setMovementMethod(LinkMovementMethod.getInstance());

可能是重复的问题,参考如何使TextView中的链接可点击? - Andy Developer
不使用Java是不可能的,因为TextView是用Java编写的。 - Miha_x64
8个回答

8

enter image description here

<TextView
        android:textSize="18sp"
        android:autoLink="web"
        android:clickable="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:text="http://www.example.com"
        tools:ignore="HardcodedText" />

不需要在布局中声明id,也不需要从Java端写代码。它可以直接在xml中使用。

使用自动链接

    • 网站链接 android:autoLink="web"
    • 电话号码链接 android:autoLink="phone"
    • 电子邮件链接 android:autoLink="email"
    • 地图链接 android:autoLink="web"
    • 所有链接 android:autoLink="all"

5

这个属性适用于TextView:android:autoLink="web"

下面是一个示例布局: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 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>

4
只需将此属性添加到您的TextView中 android:autoLink="web",例如:
<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:gravity="center"
        android:autoLink="web"
        android:clickable="true"
        android:text="https://www.google.co.in"/>

2
在TextView中添加一行:
`android:autoLink="web"` 

像这样

<TextView
        ................
        ................
        android:autoLink="email"
       .................
 />

1

正如 @Aakash Verma 建议的那样,使用 android:autoLink="web"

但是它将改变您的文本颜色。为了克服这个问题,还应该向您的 TextView 添加 android:textColorLink="your_text_color"

您的 TextView 应该像这样。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:text="https://www.google.co.in/"
    android:textColor="@color/colorPrimary"
    android:textColorLink="@color/colorPrimary" /> 

如果你仍然想用Java来做这件事。
myUrl= (TextView) rootView.findViewById(R.id.myUrl);
myUrl.setText("https://www.google.co.in/");

Linkify.addLinks(myUrl, Linkify.WEB_URLS);
myUrl.setLinkTextColor(Color.parseColor("#000000"));

EDIT:

enter image description here enter image description here

快乐编码


链接的外观如何?如果它与普通文本颜色相同,如何使其与之区分开来? - Aakash Verma
如果你只是使用 android:autoLink="web",它会将你的文本颜色更改为你的强调颜色。如果你没有指定强调颜色...它将被更改为天蓝色(默认)。android:textColorLink="your_text_color" 将作为你链接的 android:textColor。我上传了截图以便更好地理解。 - V-rund Puro-hit

0
如果您正在使用strings.xml资源中的文本,似乎会剥离html标签。因此,您必须在strings.xml中使用<![CDATA[**your string with click links**]]>将其转换为HTML,使用Html.fromHtml(string)进行转换。

0
在你的 TextView 的 XML 中使用 android:autoLink="web"。它应该会自动将 URL 变为可点击的(如果在文本中找到)。
另外,不要忘记设置 android:linksClickable="true"。我认为你不需要像你所做的那样设置 html 标签,只需要 <a href> 标签即可...

它将改变文本颜色。 - V-rund Puro-hit

0
将此添加到您的TextView中:
android:autoLink="web"
android:linksClickable="true"


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接