android - 如何在 android Textview 上使用斜线制作可点击的 url 链接
问题描述
我正在使用“setMovementMethod”在 TextView 上制作可点击的 url。最好看下面的代码:
1. String value = "By signing up, you agree to our <a href=\"https://app.mywebsite.com/terms\">My App Term</a> and confirm that you have read our <a href=\"https://app.mywebsite.com/privacypolicy\">Privacy Policy</a>";
2. TextView text = (TextView) findViewById(R.id.text);
3. text.setText(Html.fromHtml(value));
4. text.setMovementMethod(LinkMovementMethod.getInstance());
问题在于网址的“.com”之后的斜杠。如果我删除该斜线并像https://app.mywebsite.com那样编写 URL,那么它可以完美运行,但是当我像https://app.mywebsite.com/terms这样编写 URL 时,链接不是可点击。我可以看到突出显示的链接,但是当单击链接时它不起作用
我该如何解决这个问题?非常感谢。
解决方案
创建一个函数,如下所示:
fun applyHtmlToTextView(tv: TextView?, html: String) {
if (tv == null)
return
tv.movementMethod = LinkMovementMethod.getInstance()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
tv.text = Html.fromHtml(html, Html.FROM_HTML_MODE_COMPACT)
} else {
tv.text = Html.fromHtml(html)
}
}
将您的字符串添加到string.xml
文件中,如下所示:
<string name="lb_your_string"><![CDATA[<a href="https://google.com”>Google.</a>]]></string>
并通过添加代码来使用它:
applyHtmlToTextView(tv, getString(R.string.lb_your_string))
或者您可以将value
变量编辑为:
String value = "By signing up, you agree to our <a href=https://app.mywebsite.com/terms>My App Term</a> and confirm that you have read our <a href=https://app.mywebsite.com/privacypolicy>Privacy Policy</a>"
已移除\"
推荐阅读
- reactjs - reactjs复选框布尔值
- sql - 用于提取逗号分隔值的 Oracle Query
- asp.net - 通过 jquery ajax 从购物车中删除项目
- python - 在python的同一目录中访问文件夹外的文件
- javascript - 我想在背景中显示随机图像,但代码不起作用
- jekyll - Jekyll if 语句和限制
- java - 将 JavaFX 应用程序 (.Jar) 转换为 Windows 应用程序 (.exe)
- php - 遇到未捕获的异常类型:错误消息:调用未定义的函数重定向()
- python - django-filters:如何使用 ModelChoiceFilter,无法将类添加到表单字段并且无法设置表单标签
- javascript - 在 JWplayer 免费版的我的声音文件之前添加一个预卷声音