我想在TextView中显示链接,每当用户裁剪链接时,它必须打开。xml代码:
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:autoLink="web" />Java代码:
textView.setMovementMethod(LinkMovementMethod.getInstance());是的,它工作,使链接蓝色和下划线。但是当我使用一个词(例如".hello" )时,它会因为点而成为链接。因此,如果一个点和一个词是相邻的,它就变成了一个链接。我该如何解决这个问题?谢谢。
发布于 2020-10-04 14:52:16
首先从xml中删除linksClickable & autoLink属性
然后,您必须检查给定字符串中是否有任何可用的url使用regex。使用下列守则:
private boolean containsURL(String content) {
String REGEX = "\\b(https?|ftp|file)://[-a-zA-Z0-9+&@#/%?=~_|!:,.;]*[-a-zA-Z0-9+&@#/%=~_|]";
Pattern p = Pattern.compile(REGEX, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(content);
if (m.find()) {
return true;
}
return false;
}如果在给定的字符串中包含url,那么它将以蓝色显示文本。
TextView textView = findViewById(R.id.textView);
textView.setText("Any String value");
if (containsURL("Any String value")) {
Linkify.addLinks(textView, Linkify.WEB_URLS);
textView.setLinksClickable(true);
}https://stackoverflow.com/questions/64195707
复制相似问题