gpt4 book ai didi

java - 是否可以自动识别链接?

转载 作者:行者123 更新时间:2023-12-01 09:47:07 26 4
gpt4 key购买 nike

我的应用程序是一种社交应用程序。我的问题是,如果有人上传带有链接的文本,应用程序将无法识别该链接并且该链接不可单击。

我心想,也许有一种简单的方法可以让我的应用程序识别链接并使其可点击。

最佳答案

您可以使用此代码自动检测字符串。

String originalString = "Please go to http://www.stackoverflow.com";
String newString = originalString.replaceAll("http://.+?(com|net|org)/{0,1}", "<a href=\"$0\">$0</a>");

资源链接:

How to detect the presence of URL in a string

更新1:

启用对 Android 默认链接模式之一的支持非常简单。只需使用 addLinks(TextView text, int mask) 函数并指定描述所需链接类型的掩码。

import android.text.util.Linkify;

// Recognize phone numbers and web URLs
Linkify.addLinks(text, Linkify.PHONE_NUMBERS | Linkify.WEB_URLS);

// Recognize all of the default link text patterns
Linkify.addLinks(text, Linkify.ALL);

资源链接:

  1. Android Text Links Using Linkify
  2. Linkify and autoLink Need a Custom URLSpan

更多详情,您可以查看本教程:Android Linkify both web and @mentions all in the same TextView

关于java - 是否可以自动识别链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37898896/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com