0

我正在尝试插入指向 TextView 的链接,以使它们开始新的活动。我为我的Utils班级写了一个方法来做到这一点:

public static final String tagPattern = "#([^ ]+)";
public static final String tagReplace = "<a href=\"org.openihs.seendroid://display/tag/$1/\">#$1</a>";
public static final String userPattern = "@([^ ]+)";
public static final String userReplace = "<a href=\"org.openihs.seendroid://display/user/$1/\">@$1</a>";
static public void linkify(TextView view) {
    String text = view.getText().toString();
    text = text.replaceAll(Utils.tagPattern, Utils.tagReplace);
    text = text.replaceAll(Utils.userPattern, Utils.userReplace);
    view.setMovementMethod(LinkMovementMethod.getInstance());
    view.setText(Html.fromHtml(text));
    Log.d("SeenDroid", text);
    Linkify.addLinks(view, Linkify.ALL);
}

一个示例输出(到 logcat)是:

foo <a href="org.openihs.seendroid://display/tag/bar/">#bar</a> baz http://google.fr/

而真正的 UI 将http://google.fr/作为链接(预期行为)提供,但#bar作为普通文本(意外行为)。

TextView 在 ListView 中。

有什么想法可以解决这个问题吗?

问候, ProgVal

4

2 回答 2

0

不要做替换。Html.fromHtml已经正确处理标签。

于 2011-11-29T19:34:25.093 回答
0

Linkify 的工作方式与您尝试使用它的方式略有不同。

public static final String AUTHORITY = "your_package.your_content_provider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/tag/");
Pattern pattern = Pattern.compile(tagPattern);
Linkify.addLinks(view, pattern, CONTENT_URI.toString());

其中 CONTENT_URI 是指您在清单中注册的ContentProvider 。

于 2011-11-29T22:15:25.113 回答