简体   繁体   English

ClickableSpan 和 HTML 跨在同一个 TextVeiw 中

[英]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.

相关问题 从Html.fromHtml跨越,但使用自定义ClickableSpan进行自定义方案 - Spanned as from Html.fromHtml, but with custom ClickableSpan for custom scheme Android中的Spant和html格式 - Spanned and html format in Android html在android中转换为粗体或斜体的字符串 - html converted bold or italic spanned string in android 具有Spanned强制转换的Html.toHtml(ClassCastException) <Android> - Html.toHtml with Spanned cast (ClassCastException) <Android> Android HtmlCompat.toHtml(Spanned) 返回不正确嵌套的 HTML 标签 - Android HtmlCompat.toHtml(Spanned) returns improperly nested HTML tags 跨区Textview HTML格式仅适用于最后循环吗? - Spanned Textview HTML formatting only applies to last in loop? 按下时更改单个 ClickableSpan 的文本颜色,而不影响同一 TextView 中的其他 ClickableSpans - Change the text color of a single ClickableSpan when pressed without affecting other ClickableSpans in the same TextView java Android Spanned Html.fromHtml(stringWithCDATA)仍将标签显示为文本 - java Android Spanned Html.fromHtml(stringWithCDATA) still shows tags as text 使用Html.fromHtml(message)时,未定义构造函数BasicNameValuePair(String,Spanned) - The constructor BasicNameValuePair(String, Spanned) in undefined while using Html.fromHtml(message) Android:如何将 HTML 字符串转换为 Spanned 以在 TextView 中显示(必须适用于 API &lt; 24) - Android: How to convert HTML string to Spanned to display in TextView (MUST work on API < 24)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM