android - Linkify Android 转换问题

标签 android textview transform linkify

尝试做一些相当简单的事情。

像这样获取文本

User Name: This is a comment I am making

它位于单个 TextView 中。我想让用户名成为一个链接。我决定最简单的方法是用“$@”之类的东西包围用户名,这样它就变成了

"$@User Name:$@ This is a comment I am making

这样我就可以使用下面的正则表达式

Pattern userName = Pattern.compile(".*\\$@(.+)\\$@.*");

使用 Linkify 并使其成为一个链接。但是,显然我需要删除定界符,所以下面是代码

title.setText(titleText);
Linkify.TransformFilter transformer = new Linkify.TransformFilter() {

    @Override
    public String transformUrl(Matcher match, String url) {
       return match.group(1);
    }
};
Linkify.addLinks(title, userName, "content://user=", null,     transformer);

但是由于某种原因,整个文本变成了一个巨大的链接,并且文本根本没有被转换。

最佳答案

事实证明,这确实非常简单。我最终没有使用疯狂的“$@”来分隔用户名,而是坚持使用

User Name: This is a comment I am making

所以我最终使用了以下模式

Pattern userName = Pattern.compile("(.+:)");

很简单,代码就变成了

title.setText(titleText);
Linkify.addLinks(title, GlobalUtil.userName, "user://" + userId + "/");

感谢 nil 的原始建议。我确实匹配了整个字符串,而不仅仅是链接的用户名。

关于android - Linkify Android 转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6416055/

相关文章:

android - 在 android 中更改 Spinner 的外观

java.lang.NoClassDefFoundError : com. example.bylyngo

EditText 上的 Android 电子邮件验证

android - 在 ConstraintLayout 中自动调整多行 TextView

c# - 与 firebase 集成的 Unity 可在 Unity 编辑器上运行,但不适用于移动设备

android - TextView 中的文本未显示在多行中 - 它在一行中被截断

android - 为什么我不能在 textview android 中滚动?

java - 转换后获取对象在 opengl 中的位置

html - CSS3 : translateX to center element horizontally

CSS3 倾斜所有 Angular 落