Android:在字符串,SpannedStrings和Spannablestrings之间转换 [英] Android: converting between Strings, SpannedStrings and Spannablestrings

查看:60
本文介绍了Android:在字符串,SpannedStrings和Spannablestrings之间转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为"foo"的字符串资源.它可能是一个简单的字符串...或可能包含HTML.这可能会随着时间而改变:在读取是否为HTML时,我应该能够立即至少将其作为SpannableString装箱(但是如何?)

I have a string resource called "foo". It may be a simple string... or it may contain HTML. This may change over time: I should be able to box it up as at least a SpannableString immediately upon reading whether it's HTML or not (but how??)

我想获取原始的CharSequence并首先能够按原样显示(确切的字符,而不是Android的解释").现在我无法做到这一点... toString()决定剔除我不希望看到的部分.

I want to get that raw CharSequence and first be able to display it as-is (the exact characters, not Android's "interpretation" of it). Right now I can't do that... toString() decides to rip out the parts it doesn't think I want to see.

然后,我希望能够使用某种方法通过串联从此字符串和其他String或SpannableStrings创建SpannableString(正常方法都不起作用).然后,我想使用该SpannableString在TextView中显示HTML格式的文本.

I'd then like to be able to create a SpannableString from this and other Strings or SpannableStrings via concatenation using some method (none of the normal ones work). I'd like to then use that SpannableString to display the HTML-formatted text in a TextView.

这应该不难,但是显然我做的不好(到目前为止,我发现的信息很少).肯定有一种方法可以在字符串,SpannedString甚至Spannablestrings之间准确地相互转换,而又不会丢失标记吗?

This shouldn't be difficult, but clearly I'm not doing it right (there's very little info out there about this that I've found so far). Surely there is a way to accurately interconvert between between Strings, SpannedStrings and even Spannablestrings, without losing the markups along the way?

请注意,我已经使用了有点破损的Linkify,但是我想更好地控制该过程(不要晃动未格式化的"/",正确的href等).HTML可以进行所有步骤,尽管我无法连接任何东西.

Note that I've already played with the somewhat broken Linkify, but I want better control over the process (no dangling unformatted "/"s, proper hrefs, etc.) I can get this all to work IF I stay in HTML at all steps, though I can't concatenate anything.

我了解到我可以使用以下内容来确保我得到原始字符串(而不是Android认为它认为CharSequence确实是的任何东西).很好...现在,如何将其哄骗到SpannableString?

Edit 1: I've learned I can use the following to always ensure I get my raw string (instead of whatever Android decides it thinks the CharSequence really is). Nice... now, how to coax this into a SpannableString?

<string name="foo"><![CDATA[
    <b>Some bold</b>
]]>
</string>

不知道为什么它不能更早地起作用,但是...如果foo1和foo2是上面标记为字符串的字符串(如CDATA),那么显然可以做到这一点:

Edit 2: Not sure why this didn't work earlier, but... if foo1 and foo2 are strings marked up as above (as CDATA), then one can apparently do this:

String foo1 = (String)getResources().getText(R.string.foo1);
String foo2 = (String)getResources().getText(R.string.foo2);
SpannedString bar = new SpannedString(Html.fromHtml(foo1+foo2));

好奇:还有比这更直接的解决方案吗?这项CDATA业务实际上有必要吗?似乎令人费解(但不像从未完全不知道资源类型将是...字符串,可扩展性等那样令人费解)

推荐答案

似乎没有更简单的方法可以完成此任务.

It appears there isn't a more straightforward way to accomplish this.

这篇关于Android:在字符串,SpannedStrings和Spannablestrings之间转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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