【发布时间】:2012-01-09 02:13:02
【问题描述】:
我正在尝试插入指向 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 中。
有解决这个问题的想法吗?
问候, 程序验证
【问题讨论】:
标签: android hyperlink textview linkify