为什么不能在文本视图中使用 html 中的 link?
Why is not working the link from an html in a textview?
我从需要在文本视图中打印的服务获取 html。在这种情况下,html 有两个 link,第一个有效,但第二个无效。
XML:
<TextView
android:id="@+id/_doctor_comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:orientation="horizontal"
android:paddingStart="16dp"
android:paddingTop="12dp"
android:paddingEnd="16dp"
android:paddingBottom="12dp"
android:textSize="14dp"
app:textType="regular"/>
在文本视图中打印代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = Html.fromHtml(HtmlCompat.fromHtml(data.consultationData.doctorAnswer, HtmlCompat.FROM_HTML_MODE_LEGACY).toString())
txtDoctorComments.text = result
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
} else {
txtDoctorComments.text = Html.fromHtml(Html.fromHtml(data.consultationData.doctorAnswer).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
这是我从服务中得到的:
Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>
esto es otra prueba solo con el texto https://www.google.com
在这里你可以看到它在文本视图中打印的内容:
如您所见,htts://www.google.com 不会像 link 那样工作,它不会突出显示,也无法点击。如果有人知道为什么会发生这种情况,请给我任何建议。
所以有两件事。
- 您的另一个 link 刚刚添加为纯文本。它没有可以被
HtmlCompat.fromHtml
检测到的 <a href
。因此,如果您希望第二个文本也显示为 link。您必须修改响应。
示例:
val sampleText =
"Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>\n" +
"\n" +
"esto es otra prueba solo con el texto <a href="https://www.google.com">https://www.google.com</a>"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = HtmlCompat.fromHtml(
HtmlCompat.fromHtml(
sampleText,
HtmlCompat.FROM_HTML_MODE_LEGACY
).toString(), HtmlCompat.FROM_HTML_MODE_LEGACY
)
txtDoctorComments.text = result
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
} else {
txtDoctorComments.text =
Html.fromHtml(Html.fromHtml(sampleText).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
输出:
- 如果您想要一种从没有锚文本的纯文本中自动检测 link 的方法,那么您可以使用
Linkify.addLinks(txtDoctorComments,Linkify.WEB_URLS)
.
示例:
val sampleText =
"Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>\n" +
"\n" +
"esto es otra prueba solo con el texto https://www.google.com"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = HtmlCompat.fromHtml(
HtmlCompat.fromHtml(
sampleText,
HtmlCompat.FROM_HTML_MODE_LEGACY
).toString(), HtmlCompat.FROM_HTML_MODE_LEGACY
)
txtDoctorComments.text = result
Linkify.addLinks(txtDoctorComments, Linkify.WEB_URLS)
} else {
txtDoctorComments.text =
Html.fromHtml(Html.fromHtml(sampleText).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
输出:
我从需要在文本视图中打印的服务获取 html。在这种情况下,html 有两个 link,第一个有效,但第二个无效。
XML:
<TextView
android:id="@+id/_doctor_comments"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:orientation="horizontal"
android:paddingStart="16dp"
android:paddingTop="12dp"
android:paddingEnd="16dp"
android:paddingBottom="12dp"
android:textSize="14dp"
app:textType="regular"/>
在文本视图中打印代码:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = Html.fromHtml(HtmlCompat.fromHtml(data.consultationData.doctorAnswer, HtmlCompat.FROM_HTML_MODE_LEGACY).toString())
txtDoctorComments.text = result
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
} else {
txtDoctorComments.text = Html.fromHtml(Html.fromHtml(data.consultationData.doctorAnswer).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
这是我从服务中得到的:
Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>
esto es otra prueba solo con el texto https://www.google.com
在这里你可以看到它在文本视图中打印的内容:
如您所见,htts://www.google.com 不会像 link 那样工作,它不会突出显示,也无法点击。如果有人知道为什么会发生这种情况,请给我任何建议。
所以有两件事。
- 您的另一个 link 刚刚添加为纯文本。它没有可以被
HtmlCompat.fromHtml
检测到的<a href
。因此,如果您希望第二个文本也显示为 link。您必须修改响应。
示例:
val sampleText =
"Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>\n" +
"\n" +
"esto es otra prueba solo con el texto <a href="https://www.google.com">https://www.google.com</a>"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = HtmlCompat.fromHtml(
HtmlCompat.fromHtml(
sampleText,
HtmlCompat.FROM_HTML_MODE_LEGACY
).toString(), HtmlCompat.FROM_HTML_MODE_LEGACY
)
txtDoctorComments.text = result
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
} else {
txtDoctorComments.text =
Html.fromHtml(Html.fromHtml(sampleText).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
输出:
- 如果您想要一种从没有锚文本的纯文本中自动检测 link 的方法,那么您可以使用
Linkify.addLinks(txtDoctorComments,Linkify.WEB_URLS)
.
示例:
val sampleText =
"Esto es lo que debes hacer <a href="https://www.google.com">link aqui</a>\n" +
"\n" +
"esto es otra prueba solo con el texto https://www.google.com"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val result = HtmlCompat.fromHtml(
HtmlCompat.fromHtml(
sampleText,
HtmlCompat.FROM_HTML_MODE_LEGACY
).toString(), HtmlCompat.FROM_HTML_MODE_LEGACY
)
txtDoctorComments.text = result
Linkify.addLinks(txtDoctorComments, Linkify.WEB_URLS)
} else {
txtDoctorComments.text =
Html.fromHtml(Html.fromHtml(sampleText).toString())
txtDoctorComments.movementMethod = LinkMovementMethod.getInstance()
}
输出: