2011-11-29 51 views
0

我正在嘗試向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

回答

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

0

不做內容替換。 Html.fromHtml已經正確處理標籤。

+0

Html.fromHtml'如何知道我希望#tag和@user在用戶單擊它們時啓動SearchTagActivity和ShowUserActivity? –

+0

哦,等等。我忘了提供模式。第一篇文章編輯。 –