开发者

I'd like to define an Android TextView in XML which contains text which is a link to a URL

I'd like to define a TextView in XML which contains text which is a link to a URL. Essentially the semantic开发者_StackOverflow中文版s of the anchor HTML tag: <a>

I have been able to do most of what I want with, the android:autoLink="web" attribute to TextView. However, the text has to contain the URL and it is displayed to the user. I'd like to display different text which links to the URL.

Example: I can say,

<TextView android:text="http://foo.com" android:autoLink="web">

But then the user sees, "http://foo.com". I'd rather say something else, like "bar".

I tried defining the string in a string resource and using a link, such as

<string name="test"><a href="foo.com">bar</a></string>

The string shows up properly formatted, but you cannot click on it.


If you're using HTML in strings.xml for your TextView, don't use autoLink.

Instead, in your onCreate, set the movement method to use links:

    TextView msg = (TextView) findViewById(R.id.text);
    msg.setMovementMethod(LinkMovementMethod.getInstance());

I think autoLink is for when you're dynamically setting the text.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜