[英]ClickableSpan and HTML spanned in the same TextVeiw
I am attempting to implement a clickablespan in an existing HTML string in the view.我正在尝试在视图中的现有 HTML 字符串中实现 clickablespan。 The string already has href's where needed (excluding the span of characters that need to be handled programmatically).该字符串在需要的地方已经有 href(不包括需要以编程方式处理的字符范围)。 The HTML links are working as expected. HTML 链接按预期工作。
An example string:一个示例字符串:
<string name="terms_short">Message and data rates may apply. Message frequency varies. Contact customer service at <a href="tel:5555555555">(555) 555-5555</a> or via email <a href="mailto:cust_email_addr">Spiffy Email</a>. View our <a href="http://privacy_policy">Privacy Policy</a> and Terms and Conditions.</string>
In this example, displaying the HTML programmatically with clickable links is rather straight forward.在此示例中,以编程方式显示带有可点击链接的 HTML 非常简单。
val terms = getString(R.string.terms_short)
binding.textView.text = HtmlCompat.fromHtml(terms, HtmlCompat.FROM_HTML_MODE_COMPACT)
binding.textView.movementMethod = LinkMovementMethod.getInstance()
Now, I need to add a ClickableSpan to be handled internally for Terms and Conditions
.现在,我需要添加一个 ClickableSpan 以便在内部处理Terms and Conditions
。 The terms are delivered within a dialog locally.这些条款在本地对话中提供。 I have been attempting to resolve this by getting the Spanned HTML into a string builder then adding an additional ClickableSpan however this tends to stop the existing url spans function. This results in dropping the HTML links and adding only the ClickableSpan我一直试图通过将 Spanned HTML 放入字符串生成器中然后添加一个额外的 ClickableSpan 来解决此问题,但这往往会停止现有的 url 跨度 function。这导致删除 HTML 链接并仅添加 ClickableSpan
Here is what I last attempted:这是我上次尝试的:
<string name="terms_conditions">Terms and Conditions</string>
// -----------
val terms = getString(R.string.terms_short)
val sequence = HtmlCompat.fromHtml(terms, HtmlCompat.FROM_HTML_MODE_COMPACT)
val strBuilder = SpannableStringBuilder(sequence)
val termsString = getString(R.string.terms_conditions)
strBuilder.setSpan(
object : ClickableSpan() {
override fun onClick(widget: View) {
Timber.d("Click handled internally")
}
},
sequence.indexOf(termsString),
sequence.indexOf(termsString) + termsString.length,
SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE
)
binding.textView.text = strBuilder
binding.textView.movementMethod = LinkMovementMethod.getInstance()
The linking didnt work due to getString()
stripping the html tags out of the string.由于getString()
从字符串中剥离了 html 标签,因此链接不起作用。 The HTML was working as expected prior because the resource was defined in the layout property not programmatically as shown here. HTML 之前按预期工作,因为资源是在布局属性中定义的,而不是以编程方式定义的,如此处所示。
<string name="terms_short"><![CDATA[ Message and data rates may apply. Message frequency varies. Contact customer service at <a href="tel:5555555555">(555) 555-5555</a> or via email <a href="mailto:cust_email_addr">Spiffy Email</a>. View our <a href="http://privacy_policy">Privacy Policy</a> and Terms and Conditions.]]></string>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.