Android Spanned、SpannedString、Spannable、SpannableString 和 CharSequence [英] Android Spanned, SpannedString, Spannable, SpannableString and CharSequence

查看:10
本文介绍了Android Spanned、SpannedString、Spannable、SpannableString 和 CharSequence的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android 提供了各种与文本和字符串相关的界面:SpannedSpannedStringSpannableSpannableStringCharSequence.

Android offers a variety of interfaces all related to text and strings: Spanned, SpannedString, Spannable, SpannableString and CharSequence.

在使用 Html.fromHtml()TextView 中显示可链接文本之后,我在各种场景中通常使用了上述所有方法为了对其应用一些样式.

I have used all of the above in various scenarios usually after using Html.fromHtml() to display linkable text inside a TextView in order to apply some styling to it.

我试图从 Android 的官方文档中了解这些接口的用途/用法,但由于令人困惑而失败了.这些接口的目的是什么?在哪些情况下最常使用它们?在哪些情况下最好避免使用它们?使用其中任何一种时,是否有任何明显的性能影响需要考虑?

I have tried to understand the purpose/usage of these interfaces from Android's official documentation and have failed as it is quite confusing.. What is the purpose of these interfaces? in which scenarios is it mostly common to use them? In which cases is it best to avoid using them? Are there any obvious performance impacts to be considered when using any one of them?

如果有人能提供一个体面的解释,将不胜感激.

If anyone could provide a decent explanation it would be much appreciated.

推荐答案

这些接口的用途是什么?

What is the purpose of these interfaces?

CharSequence 是表示字符序列的标准 Java 接口.StringCharSequence 最常用的具体实现,其次是 StringBuilder.

CharSequence is a standard Java interface representing a sequence of characters. String is the most commonly-used concrete implementation of CharSequence, followed by StringBuilder.

Spanned 是一个带有spans"的 CharSequence,表示应用于文本部分的格式,这些跨度不能被修改.

Spanned is a CharSequence with "spans" indicating formatting to apply to portions of the text, where those spans cannot be modified.

Spannable 是一个 Spanned,增加了修改 span(添加或删除格式)的能力,但 修改文本本身.

Spannable is a Spanned, adding in the ability to modify the spans (to add or remove formatting), but not to modify the text itself.

SpannedStringSpanned 接口的具体实现.

SpannedString is a concrete implementation of the Spanned interface.

SpannableStringSpannable 接口的具体实现.

SpannableString is a concrete implementation of the Spannable interface.

在哪些场景中最常使用它们?

in which scenarios is it mostly common to use them?

当有一个方法返回一个时(例如,EditText 上的 getText())或当有一个方法将一个作为参数时(例如,setText()TextView 上).

When there is a method that returns one (e.g., getText() on an EditText) or when there is a method that takes one as a parameter (e.g., setText() on a TextView).

您引用的使用 Html.fromHtml() 的案例可能是传统 Android 开发中最常见的,作为带有 SpannedTextView重量比 WebView 轻得多.但是,还有其他用例,例如:

Your cited case of using Html.fromHtml() is perhaps the most common in conventional Android development, as a TextView with a Spanned is much lighter in weight than is a WebView. However, there are other use cases, such as:

允许用户输入富文本,然后使用Html.toHtml() 将格式化的文本保存在 HTML 再现中

Allowing users to enter in rich text, then using Html.toHtml() to persist that formatted text in an HTML rendition

在哪些情况下最好避免使用它们?

In which cases is it best to avoid using them?

它们在对抗脱发、除雪、热泵维修、制作蛋奶酥等方面非常糟糕.

They are singularly awful at combating baldness, snow removal, heat pump repair, making a soufflé, etc.

:-)

在使用其中任何一种时,是否有任何明显的性能影响需要考虑?

Are there any obvious performance impacts to be considered when using any one of them?

根据定义,接口没有性能影响"——它们只是对 API 的描述.

Interfaces, by definition, do not have "performance impacts" -- they are merely a description of an API.

我不知道 SpannableString 在任何特定操作中都比 SpannedString 慢得多.但是,SpannableStringBuilder(除了格式化文本的 span 之外还允许操作文本)可能比 SpannableStringSpannedString 适用于各种事物.不过,性能差异是否足够重要将取决于使用情况.

I am not aware that SpannableString is significantly slower than SpannedString at any particular operation. However, SpannableStringBuilder (which allows for manipulating the text in addition to the spans that format that text) may well be a bit slower than SpannableString or SpannedString for various things. Whether or not the performance differences are enough to matter will depend on usage, though.

这篇关于Android Spanned、SpannedString、Spannable、SpannableString 和 CharSequence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆