目前我正在使用以下方法从字符串中获取 URL 并插入到 TextView 中。它工作正常,但确实看到有人对这种方法有任何问题吗?
public String StringURL(String args) {
String s = args;
String [] parts = s.split("\\s");
String withURL = "";
for( String item : parts ){
if (Patterns.WEB_URL.matcher(item).matches()) {
if (!item.startsWith("http://") && !item.startsWith("https://")){
item = "http://" + item;
}
withURL += "<a href=\"" + item + "\">"+ item + "</a> ";
}
else {
withURL += item + " ";
}
}
return withURL;
}
在 TextView 中,我按以下方式设置返回的字符串:
TextView.setText(Html.fromHtml(StringURL(P)));
TextView.setMovementMethod(LinkMovementMethod.getInstance());
此方法仅适用于 SdkVersion 8+,因为 Patterns.WEB_URL 从 SdkVersion 8 开始引入。