简体   繁体   English

如何使我的TextView可点击

[英]How to make my TextView clickable

I am trying to make my TextView clickable. 我试图使我的TextView可点击。 I have set setMovementMethod() with LinkMovementMethod. 我已经用LinkMovementMethod设置了setMovementMethod()。

myText.setMovementMethod(LinkMovementMethod.getInstance());

But when I set myText value to 'cnn.com/money' or 'www.cnn.com/money'.Both are not clickable. 但是当我将myText值设置为'cnn.com/money'或'www.cnn.com/money'时,两者均不可单击。 How can I make the text clickable (ie go to web browser for the link)? 如何使文本可点击(即,转到Web浏览器以获取链接)?

Thank you. 谢谢。

Use Linkify: 使用Linkify:

myText.setAutoLinkMask(0)
Linkify.addLinks(myText, Linkify.ALL);

If you just wanted to have weblinks in your textview be picked up, (ie not email addresses or phone numbers), then do: 如果您只是想在textview中获取Web链接(即不接收电子邮件地址或电话号码),请执行以下操作:

Linkify.addLinks(myText, Linkify.WEB_URLS);

See here for more cool stuff you can do with linkify. 请参阅此处,以了解可以使用linkify做的更多有趣的事情。

I would add 我会补充

android:clickable="true"

to the XML for the TextView, then add an onClick handler pragmatically that pulls the text from the TextView and fires off a browser intent with it to be picked up by whatever browser the user chooses. 到TextView的XML中,然后实用地添加一个onClick处理程序,该处理程序从TextView中提取文本并触发浏览器意图,以供用户选择的任何浏览器接收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM