我正在嘗試向TextView插入鏈接以使它們開始新的活動。我寫了一個方法,我Utils
類做到這一點:無法在TextView中插入自定義鏈接
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
Html.fromHtml'如何知道我希望#tag和@user在用戶單擊它們時啓動SearchTagActivity和ShowUserActivity? –
哦,等等。我忘了提供模式。第一篇文章編輯。 –