Android 有没有关于 Spanned 和 Spannable 文本的例子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2159847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:45:34  来源:igfitidea点击:

Is there any example about Spanned and Spannable text

androidandroid-widget

提问by skysign

I'm struggling with using EditText and Spannable text object, These days, I've read API documents around ten times, even I'm not certain that I understand correctly. So I'm looking for a kind of example which show me how to utilize EditText and Spannable.

我正在努力使用 EditText 和 Spannable 文本对象,这些天来,我已经阅读了大约十次 API 文档,即使我不确定我是否理解正确。所以我正在寻找一种示例,向我展示如何使用 EditText 和 Spannable。

回答by andy

Since you don't specify what you can't grasp from the API it's hard to answer your questions (short answer: rewrite your question to a specific questions rather than a general one).

由于您没有指定无法从 API 中掌握的内容,因此很难回答您的问题(简短回答:将您的问题重写为特定问题而不是一般问题)。

A typical Spannable-example is something like this to turn selected text in an EditText into Italic:

一个典型的 Spannable-example 是这样的,它将 EditText 中的选定文本转换为斜体:

Spannable str = mBodyText.getText(); 
if(mBodyText.getSelectionEnd() > mBodyText.getSelectionStart()) 
  str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),  
                      mBodyText.getSelectionStart(), mBodyText.getSelectionEnd(),  
                      Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
else
  str.setSpan(new StyleSpan(android.graphics.Typeface.ITALIC),
              mBodyText.getSelectionEnd(),
              mBodyText.getSelectionStart(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

This is cut and pasted from something else, so your direct-pastability might have suffered, but it at least shows a working example of a Spannable(in this case a StyleSpan). In the API you can find the other types of Spans (notably ImageSpan, which is a common questions among newly converted droiders).

这是从其他东西剪切和粘贴的,因此您的直接粘贴性可能会受到影响,但它至少显示了 a Spannable(在本例中为 a StyleSpan)的工作示例。在 API 中您可以找到其他类型的 Span(特别是ImageSpan,这是新转换的机器人中的常见问题)。

回答by Sam L.

I'm just starting to try to figure it out too, and it seems unnecessarily tricky.

我也刚开始尝试弄明白,这似乎不必要地棘手。

Here's a working method to add NEW spannable text to an existing view. I wanted to add colored text to a view, and this seemed like the only way to do it.

这是将新的可跨文本添加到现有视图的工作方法。我想在视图中添加彩色文本,这似乎是唯一的方法。

Though it feels like an ugly hack, you can create a dummy TextView (not shown anywhere) and style the text there, then append that styled text to wherever you want. Credit for it goes to iifuzz at anddev.org. My code looks like so:

虽然感觉像是一个丑陋的黑客,但您可以创建一个虚拟的 TextView(未在任何地方显示)并在那里设置文本样式,然后将该样式文本附加到您想要的任何位置。归功于它在 anddev.org 上的 iifuzz。我的代码看起来像这样:

    spanbuffer = new TextView(context);
    spanbuffer.setText(newText, TextView.BufferType.SPANNABLE);
    Spannable s = (Spannable) spanbuffer.getText();
    s.setSpan(new ForegroundColorSpan(Color.RED), 0, newText.length() - 1, 0);
    this.append(s);

I think you're supposed to be able to create new spannable text using the SpannableFactory, like so:

我认为您应该能够使用 SpannableFactory 创建新的可跨文本,如下所示:

    Spannable s = Spannable.Factory.getInstance().newSpannable(newText);

but I couldn't get this text to actually show new span effects, so I gave up.

但是我无法让这个文本真正显示新的跨度效果,所以我放弃了。