我的应用程序从API加载注释,这些注释通常包含与Stack Overflow上相同标记的链接。 (如果有这个标记样式的名称来帮助我谷歌,请在评论中告诉我)
//this is the markup I am referring to
[Here's a picture](https://www.web.com/sub/path/to/picture/?st=JHTYA46&am-p;sh=487Bac48)
我尝试将它们转换为
的链接private static final String REGEX_LINK_MARKUP = "\\[(.*?)\\]\\((.*?)\\)";
private static final String REGEX_LINK_REPLACEMENT = "<a href=\"$2\">$1</a>";
commentText.setText(comment.getBody().replaceAll(REGEX_LINK_MARKUP, REGEX_LINK_REPLACEMENT)));
并使用
android:autoLink="all"
但是当然,这显示HTML可以点击href部分,所以我目前正在将它们转换为链接
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
//the constants are the same patterns from above
commentText.setText(Html.fromHtml(comment.getBody().replaceAll(REGEX_LINK_MARKUP, REGEX_LINK_REPLACEMENT), Html.FROM_HTML_MODE_LEGACY));
} else {
commentText.setText(Html.fromHtml(comment.getBody().replaceAll(REGEX_LINK_MARKUP, REGEX_LINK_REPLACEMENT)));
}
我现在看到正确颜色的跨度,但它不可点击。该字段已经
android:linksClickable="true"
我是否将以下内容留给none
或all
没有任何区别(链接无法点击)
android:autoLink="none"
答案 0 :(得分:3)
这是一个使用替换和拆分的建议:
public static String getFormatted(String rawInput){//rawInput is the link. It does not find links from a bulk of text.
String[] content;
content = rawInput.split("\\]\\(");//Split the input
String ctnt = content[0].replaceAll("\\[", "");//remove remaining link bracket
String link = content[1].replaceAll("\\)", "");
String format = "<a href=\"%s\">%s</a>";//The format
return String.format(Locale.ENGLISH, format, link, ctnt);//Format and return the link in the format of an `a`-tag
}
呼叫:
System.out.println(getFormatted("[This is link text](https://example.com)"));//System.out.println is to show it works
返回:
<a href="https://example.com">This is link text</a>
要将链接设置为可点击,您只需执行以下操作(source):
TextView t2 = (TextView) findViewById(R.id.text2);
//Set text if you do that. If you do set the text in Java code, make sure
//you add Html.fromHtml() to allow it. You may need to add <p> tags as well
t2.setMovementMethod(LinkMovementMethod.getInstance());
这样,您就可以在文本视图中使用<a href"...
并使其可点击
有没有办法在不使用HTML的情况下制作TextView链接?
根据我所看到的,如果不显示完整链接,您就无法做到这一点(例如:www.google.com)。
编辑:
要获取任何给定文本并重新格式化,请执行以下操作:
String pattern = "(\\[.*\\]\\(.*\\))";
Pattern p = Pattern.compile(pattern);
String link = "Blah blah [link](https://example.com) blah blah";//This will be replaced with your block of text
link.replaceAll(pattern, "%s");
Matcher matcher = p.matcher(link);
String lnk = null;
if (matcher.find()){
lnk = matcher.group(1);
}
System.out.println(lnk);//Debug
String formatted = getFormatted(lnk);
System.out.println(formatted);//Debug
link = String.format(Locale.ENGLISH, link, formatted);//This amends the formatted link into the location of the [link](in this format)
它使用了本答案顶部提供的方法。
打印出来:
[link](https://example.com)//The link extracted from the block
<a href="https://example.com">link</a>//The formatted link
答案 1 :(得分:1)
如果你必须考虑转义分隔符,一般的正则表达式将是
\[([^\\\]]*(?:\\[\S\s][^\\\]]*)*)\]\(([^\\)]*(?:\\[\S\s][^\\)]*)*)\)
扩展
\[
( # (1 start)
[^\\\]]*
(?: \\ [\S\s] [^\\\]]* )*
) # (1 end)
\]
\(
( # (2 start)
[^\\)]*
(?: \\ [\S\s] [^\\)]* )*
) # (2 end)
\)