android-文本视图自动链接
我想在TextView中显示链接,当用户点击链接时,它必须打开。 xml代码:android-文本视图自动链接,android,xml,android-layout,textview,Android,Xml,Android Layout,Textview,我想在TextView中显示链接,当用户点击链接时,它必须打开。 xml代码: <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:linksClicka
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:linksClickable="true"
android:autoLink="web" />
是的,它的工作,使链接蓝色和下划线。但当我使用一个单词,例如“.hello”时,它会因为点而变成一个链接。因此,如果一个点和一个单词相邻,它就成为一个链接。我怎样才能解决这个问题?
谢谢。首先从xml中删除linksClickable和autoLink属性 然后,您必须检查给定字符串中是否存在使用regex的可用url。 使用以下代码:
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);
}
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);
}