- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的应用程序从一个 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"
最佳答案
这是使用替换和拆分的建议:
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/
使用 htaccess 我能够隐藏文件扩展名,使其成为/home 而不是/home.html。但是,对于主页,我想完全隐藏文件名,例如 stackoverflow.com,而不是 stackoverf
我试图在关闭子窗体时关闭我的主(父)窗体。然而,这给了我一个 StackOverflow 异常。 但是,如果我在 FormClosed 事件上调用 _child.Dispose,它会按预期工作。我应该
我在 IDEA 中使用 Gradle 并得到以下输出。 * What went wrong: A problem occurred evaluating script. > java.lang.Sta
我是一个 java 新手,并且在 StackOverflow 错误/在类之间访问文件的能力方面遇到了一个非常令人困惑的问题。我知道根本原因可能是我进行了一些递归调用,但修复它的语法却让我无法理解。我认
我需要编写一个程序来生成一些随机数,而不让两个数字落在某个范围内。不幸的是,我需要在学校做这件事,而我在计算机上唯一可以使用的语言是 Java(否则我会用 C++ 来做)。我对 Java 知之甚少,我
尝试设置一个 Person 类 我在尝试将信息封装在类中以使其不会被意外更改时遇到了问题。除了当我尝试使用 setter/getters 进行封装时,该类工作得非常好。我认为问题是这些方法最终会互相循
使用 scala,我已将大约 100000 个节点添加到链表中。当我使用函数 length 时,例如 mylist.length。我收到“java.lang.StackOverflowError”错误
我正在开发一个学校项目,该项目必须有 3 个带 1 个指针的快速排序、3 个带 2 个指针和 2 个堆的快速排序。 到目前为止,我已经编写了快速排序和堆之一。我遇到的问题是快速排序在小情况下工作正常,
我正在制作一个程序,它生成列表的所有子集,然后排列所有这些子集。我有一个生成子集的方法和一个生成排列的方法。但是,它们不能一起工作,即当我排列子集时,我没有得到子集的所有排列,并且出现 StackOv
我有以下函数,它应该产生笛卡尔平面中的所有坐标,我可以在n步中从原点到达: 原点是“位置”,步数是“强度”,它是一个 1-10 的整数。但是,我不断收到 stackoverflow 错误。每次我调用它
当我尝试使用 scala lib(使用 re2)匹配上述正则表达式时,代码进入以下路径并超时 1 分钟: 正则表达式: (([a-z0-9!#$%&'*+?^_`{|}~-]+(?:.[a-z0-9!
我正在开发一个运输 map 项目。我为该位置创建了一个名为 Chart 的对象和一个名为 Pos 的对象。我无法开始工作的是,当我从文本文件读取图表以正确创建对象 Pos 时,因为这就是失败的地方。
public class SortAnimal { public static Comparator getAnimalTagComparator() { return
我因使用以下泛型而收到 StackOverflowError: 积分: package me.expdev.gkitpvp; /* * Project created by ExpDev */ i
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我为一些命题逻辑表达式编写了一个javaCC解析器。表达式可能会很长,有 30k 个字符。 当我解析如此大的表达式时,出现堆栈溢出异常。 是否有一些虚拟机参数决定堆栈大小? 或者在这种情况下你会怎么做
这个问题已经有答案了: What is a StackOverflowError? (15 个回答) 已关闭 8 年前。 最高数字double可以代表极高,我想。尽管下面的代码会抛出异常。这实际上是我
我正在为我的 API 构建一个 SDK,其中有两个模块,分别是 A 类和 B 类。 public class A{ public final B = new B(); public f
执行此递归时,我收到堆栈溢出错误。有一个模式,它第一次说: at MazeGui.move(MazeGui.java:79) which is line if(rigting.goal == true
我编写的这段代码在达到一定的输入大小之前效果很好。如果输入太大,我会收到“java.lang.StackOverflowError”。我已经阅读了 stackoverflow 上有关此主题的其他一些条
我是一名优秀的程序员,十分优秀!