android - 我如何在 Android 上像 StackOverflow 使用的那样链接标记?

标签 android regex

我的应用程序从一个 API 加载评论,该 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"

当然这显示了带有 href 部分的 HTML 可点击,所以我目前正在将它们转换为链接

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"
  • 在 TextView 中使此类标记可点击的正确方法是什么?
  • 有没有一种方法可以在不使用 HTML 的情况下制作 TextView 链接?
  • 有没有比我的基本模式更有效的正则表达式?

最佳答案

这是使用替换和拆分的建议:

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"...在 TextView 中并使其可点击

is there a way to make TextView links without using HTML?

根据我所见,如果不显示完整链接(例如: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

关于android - 我如何在 Android 上像 StackOverflow 使用的那样链接标记?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43399631/

相关文章:

android - 我怎样才能让后退按钮关闭 flutter WebView 插件应用程序在 android 上

安卓搜索栏风格

android - MediaSession 和 MediaSessionManager 是否向后兼容?

java - 在java中使用正则表达式分隔自定义标签的内容

java - 基于 JSF 的触摸网站框架

正则表达式:在 "Title"数据库字段中匹配 Mr. Ms. etc

regex - Grok 调试器和 Logstash grok 中的不同行为

python - 匹配另一个 url - 正则表达式 django urls

python - 在 Python 文本语料库中优化正则表达式搜索

android - 无法在内部类线程中将文本设置为 EditText 或 TextView