我希望能够将格式化文本(即具有格式跨度的文本)从我的一个应用 Activity 发送到另一个应用 Activity 。这些 CharSequence
实例深入到我创建的一些 Parcelable
类型中。
例如,当编码其中一种携带格式化的 CharSequence
的类型时,我使用 TextUtils.writeToParcel
如下:
public class HoldsCharSequence {
/* some formatted string that uses basic spans (e.g., ForegroundColorSpan) */
private CharSequence cs;
public void writeToParcel(Parcel out, int flags) {
TextUtils.writeToParcel(cs, out, 0);
}
/* ... rest of the code ... */
}
问题是我不知道如何在我的私有(private)构造函数中从 Parcel
中检索 CharSequence
。
以下不不工作:
private HoldsCharSequence(Parcel in) {
cs = (CharSequence) in.readValue(CharSequence.class.getClassLoader());
}
我收到以下错误:
android.os.BadParcelableException: ClassNotFoundException when unmarshalling
还有两件事:
1. 我已经成功实现了我自己的自定义 Parcelable
对象,问题尤其在于 CharSequence
。
2. 我知道 TextUtils.writeToParcel
会尽最大努力保存文本格式,我可以接受。
最佳答案
如果其他人感兴趣,我在 this post 中找到了答案.
使用 TextUtils.writeToParcel(...)
检索存储在 Parcel
中的 CharSequence
的正确方法是
cs = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
关于android - 如何从包裹中检索使用 TextUtils.writeToParcel(...) 保存的 CharSequence?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9916044/