有这样一个问题,调用TextView的setText(CharSequence text)方法设置一个SpannableString,但是通过getText()得到的mText并不是SpannableString的实例, 而是一个SpannedString对象。查看TextView的源码发现,setText(CharSequence text)最终会调用下面的 setText()方法,mBufferType的默认值是BufferType.NORMAL,走到最后一个if else分支里,所以最后会调用TextUtils的stringOrSpannedString方法。
private void setText(CharSequence text, BufferType type,
boolean notifyBefore, int oldlen) {
...
if (type == BufferType.EDITABLE || getKeyListener() != null
|| needEditableForNotification) {
...
} else if (precomputed != null) {
...
} else if (type == BufferType.SPANNABLE || mMovement != null) {
text = mSpannableFactory.newSpannable(text);
} else if (!(text instanceof CharWrapper)) {
text = TextUtils.stringOrSpannedString(text);
}
}
复制代码
在这个函数里可以看到只要是Spanned的对象都会被转成SpannedString,这个SpannedString被赋值给mText。
public static CharSequence stringOrSpannedString(CharSequence source) {
if (source == null)
return null;
if (source instanceof SpannedString)
return source;
if (source instanceof Spanned)
return new SpannedString(source);
return source.toString();
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


















![[02/27][官改] Simplicity@MIX2 ROM更新-一一网](https://www.proyy.com/wp-content/uploads/2020/02/3168457341.jpg)


![[桜井宁宁]COS和泉纱雾超可爱写真福利集-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/4d3cf227a85d7e79f5d6b4efb6bde3e8.jpg)

![[桜井宁宁] 爆乳奶牛少女cos写真-一一网](https://www.proyy.com/skycj/data/images/2020-12-13/d40483e126fcf567894e89c65eaca655.jpg)