Android的跨区,SpannedString,Spannable,SpannableString和的CharSequenceSpannedString、Android、Spannable、Char

由网友(爷爱上你√)分享简介:Android提供了多种接口的所有文字和字符串相关的:跨区, SpannedString , Spannable , SpannableString 和的CharSequence 。我都用上述的各种方案中的一般使用后 Html.fromHtml()显示在的TextView 以一些样式应用到它。我试图了解这些接口从A...

Android提供了多种接口的所有文字和字符串相关的: 跨区 SpannedString Spannable SpannableString 的CharSequence

我都用上述的各种方案中的一般使用后 Html.fromHtml()显示在的TextView 以一些样式应用到它。

我试图了解这些接口从Android的官方文档的目的/使用和失败,因为它是相当混乱.. 这些接口的目的是什么?在这种情况下是它主要是共​​同使用它们?在这情况下,它是最好避免使用它们?是否有使用它们中的任何一个时,要考虑任何明显的性能影响?

如果任何人都可以提供一个体面的解释,这将是更AP preciated。

感谢您

解决方案   

这些接口的目的是什么?

的CharSequence 是一个标准的Java接口重新presenting字符序列。 字符串是最常用的具体实施的CharSequence ,其次是的StringBuilder

跨区的CharSequence 与跨越,表示要应用的格式的文本部分,其中的跨度不能修改

Spannable 跨区,在修改跨度的能力增加(添加或删除格式),但是的没有的修改文本本身。

SpannedString 是一个具体的实施跨区接口。

SpannableString 是一个具体的实施 Spannable 接口。

  java4android string –

在这情况下是它主要是共​​同使用它们?

当有一个返回一(例如,的getText()上的的EditText ),或当一个方法有是一种方法,它接受一个作为一个参数(在例如,的setText() 的TextView )。

您提到使用的情况下, Html.fromHtml()也许是最常见的传统Android开发,作为的TextView 跨区在重量上要轻得多比是的WebView 。但是,也有其他用途的情况下,如

突出显示搜索结果

允许用户富文本进入,然后用 Html.toHtml( )来坚持在一个HTML再现了格式的文本

  

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

他们奇异可怕的打击秃头,除雪,热泵维修,制作蛋奶酥等。

: - )

  

有没有使用其中任何一个时,要考虑任何明显的性能影响?

接口,顾名思义,不具备业绩的影响。 - 它们只不过是一个API的说明

我不知道 SpannableString SpannedString 显著慢于任何特定的操作。然而, SpannableStringBuilder (它允许除了那格式化文本跨度操纵文本)可能比 SpannableString 或 SpannedString 的各种事情。无论是否性能差异就足以问题将取决于使用情况,虽然。

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

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.

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.

Thank you

解决方案

What is the purpose of these interfaces?

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 is a CharSequence with "spans" indicating formatting to apply to portions of the text, where those spans cannot be modified.

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

SpannedString is a concrete implementation of the Spanned interface.

SpannableString is a concrete implementation of the Spannable interface.

in which scenarios is it mostly common to use them?

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).

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:

Highlighting search results

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?

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

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.

相关推荐