开发者

Changing styles in a TextView: can you call getText and then setSpan?

开发者 https://www.devze.com 2023-03-06 23:03 出处:网络
Can you call getText() on a TextView, and then call setSpan() on the resulting object?I know it works (at least on the phones I\'ve tried in 2.2 and 2.3), but the doc for getText() clearly says that y

Can you call getText() on a TextView, and then call setSpan() on the resulting object? I know it works (at least on the phones I've tried in 2.2 and 2.3), but the doc for getText() clearly says that you cannot modify the result of getText().

setSpan sure looked like a modification, but after carefully reading the documentation I'm not so sure:

setSpan(Object what, int start, int end, int flags)
Attach the specified markup object to the range start…end of the text, or move the object     to that range开发者_C百科 if it was already attached elsewhere.

I can read that as saying that the only change is to the markup object (in which case you're not violating the doc condition of no mods to the Spannable).

Is this a bad hack that will break like a twig in the next release? Or is calling setSpan() somehow exempt from the "don't change this" doc for getText()?

(Another update: the FAQ explicitly says to do this: http://developer.android.com/resources/faq/commontasks.html, so I'm thinking it's just misleading doc.)


TextView.getText() returns CharSequence, not Spannable, so you can't assume that the result will be Spannable. Even if it is Spannable, changing spans will not affect TextView's visual representation. And certainly such assumptions shouldn't be made because Android is open source system and every manufacturer can modify it somehow. So you shouldn't rely on implementation details.

0

精彩评论

暂无评论...
验证码 换一张
取 消