Android-是否可以在字符串资源中添加可点击的链接
我通常会设置某种 AlertDialog
在用户第一次使用我的应用程序时触发,我会解释如何使用该应用程序并对他们刚刚下载的内容进行总体介绍。我通常还从 strings.xml
文件加载字符串。
我想要做的是使我的字符串资源中的单词之一可以像网页上的超链接一样单击。基本上,您将有一个AlertDialog
,并且在字符串资源中会有一个突出显示的单词,或者可能只是一个他们可以按下的网址。我想我可以添加一个按钮将它们带到该网站,但我只是想知道是否可以在字符串资源中创建一个单词作为可点击的超链接。
I usually set up some kind of AlertDialog
to fire off when a user first uses one of my apps and I explain how to use the app and give an overall introduction to what they just downloaded. I also usually load my strings from a strings.xml
file.
What I want to do is make one of the words in my string resource clickable like a hyperlink on a web page. Basically you'd have an AlertDialog
and within the string resource there would be a highlighted word or possibly just a web address that they could press. I suppose I could just add a button that would take them to the site but I just wanted to know if making a word in your string resource a clickable hyperlink was possible.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需在资源中使用 HTML 格式链接:
然后,您可以在
TextView
上使用setMovementMethod(LinkMovementMethod.getInstance())
以使链接可点击。还有
TextView
的android:autoLink
属性也应该起作用。Just use an HTML format link in your resource:
<string name="my_link"><a href="http://somesite.com/">Click me!</a></string>
You can then use
setMovementMethod(LinkMovementMethod.getInstance())
on yourTextView
to make the link clickable.There is also
TextView
'sandroid:autoLink
attribute which should also work.我发现了一些有趣的事情。如果有人注意到这一点,请告诉我。
如果您使用以下超链接则不起作用
,但可以使用
但如果您使用以下链接则可以同时使用
I found something interesting. Let me know if any of you observed this.
Below hyperlink is not working if you use
but works with
but if you use the following link it works with both
正如 @Nikolay Elenkov 所回答的,在 Kotlin 中,我以这种方式从字符串资源中使用它(新手的详细方式):
在我的布局中放置一个复选框:
strings.xml 中
在 onCreate( 内的
activity
类中的 ) 方法:As answered by @Nikolay Elenkov In Kotlin I used it from string resources in this way (detailed way for freshers):
in my layout place a checkbox:
in strings.xml
in my
activity
class inside the onCreate() method:对我来说,超链接总是工作得很好,当我:
For me, the hyperlink always works well, when I:
<string name="link"><![CDATA[<a href="https://google.com">Google link</a>]]></string>
Android 不会自动使包含有效链接的字符串可点击。您可以做的是将自定义视图添加到对话框中并使用 WebView 来显示警报消息。在这种情况下,您可以将 html 存储在资源中,并且它们将是可点击的。
警报对话框布局.xml
Android doesn't make strings that contain valid link clickable automatically. What you can do, is add custom view to your dialog and use WebView to show the alert message. In that case, you can store html in your resources and they will be clickable.
alert_dialog_layout.xml