如何使用 Parcel.readBooleanArray()?

发布于 2024-12-01 16:27:53 字数 291 浏览 5 评论 0原文

我正在尝试使用 android.os.Parcel 中的 readBooleanArray,但 readBooleanArray 返回 void,因此我不清楚如何使用此方法。

我正在使用以下方法向 Parcel 写入一些内容:

public void writeToParcel(Parcel out, int flags) {
    out.writeBooleanArray(new boolean[] {value});
}

如何在 Parcelable 构造函数中获取此值?

I'm trying to use the readBooleanArray from android.os.Parcel, but readBooleanArray returns void and therefor it's unclear to me how to use this method.

I'm using the following method to write something to the Parcel:

public void writeToParcel(Parcel out, int flags) {
    out.writeBooleanArray(new boolean[] {value});
}

How should this value be obtained in the Parcelable constructor?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-12-08 16:27:53

我相信您需要传递一个 boolean[],Parcel 中的值将被复制到其中,然后您从该数组中读取。

示例代码:

boolean[] myBooleanArr = new boolean[1];
parcel.readBooleanArray(myBooleanArr);
boolean value = myBooleanArr[0];

I believe you need to pass a boolean[], the values in the Parcel will be copied to that, then you read from that array.

Sample code:

boolean[] myBooleanArr = new boolean[1];
parcel.readBooleanArray(myBooleanArr);
boolean value = myBooleanArr[0];
浮华 2024-12-08 16:27:53

如果您有一个真正的布尔数组,而不仅仅是一个值,并且您不知道数组的长度,那么您可以使用 createBooleanArray() 而不是 readBooleanArray(boolean[])。它将返回一个新数组,该数组与您使用 writeBooleanArray(boolean[]) 放入包中的数组相同。

If you have a real boolean array, not only one value, and you don't know the length of the array, then you can use createBooleanArray() instead of readBooleanArray(boolean[]). It will return a new array which is the same you put into the parcel with writeBooleanArray(boolean[]).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文