我想運用以下代碼給textview添加一個鏈接:
TextView textview = (TextView) findViewById(R.id.mytext);
Pattern myPattern = Pattern.compile("WordToBeLinked");
String link = "http://mydomain.com/something";
Linkify.addLinks(textview, myPattern, link);
預期實現的正常的運行結果應該是:WordToBeLinked可以鏈接,根據這個鏈接可以打開浏覽器。但不知為什麼使用Linkify給WordToBeLinked添加鏈接到URL後, URL會被這樣調用:
http://mydomain.com/somethingWordToBeLinked
是哪裡出錯了呢?
你應該使用TransformFilter。希望對你有幫助:
TextView textview = (TextView) findViewById(R.id.mytext);
textview .setText("WordToBeLinked");
TransformFilter mentionFilter = new TransformFilter() {
public final String transformUrl(final Matcher match, String url) {
return new String("http://mydomain.com/something");
}
};
Pattern pattern = Pattern.compile(".");
String scheme = "";
Linkify.addLinks(textview, pattern, scheme, null, mentionFilter);