屏幕方向改变迫使数据重新输入

发布于 2024-10-12 09:09:44 字数 368 浏览 4 评论 0原文

我正在开发一个简单的应用程序,只有少数活动,没有什么复杂的。我遇到的问题是,在我的一些活动中,我有 EditText 小部件,当屏幕旋转时,这些小部件会丢失它们的值。它们仍然在视觉上(即在手机屏幕上)报告旋转之前的值,但向代码报告它们的默认值。

举个例子,我有一个带有 EditText 的活动,我输入一个数字 15,计算器开始计算,显示结果。如果此时我旋转到不同的方向,正如预期的那样,活动死亡,重新加载,显示新的方向,我的值 15 仍然在 EditText 中,但是计算器(代码)计算得好像它是 0,这是初始值。我还没有尝试更改初始值来查看它是否真的从默认值中拉出,或者只是在将其转换为 int 时将其自身设置为 0。

如果我解释得不够好,请告诉我,如果需要,我可以添加源代码。 谢谢。

I'm developing a simple app, only a handful of activities and nothing to complicated. The issue I'm having is that in a few of my activities I have EditText widgets, and when the screen is rotated, these widgets loose their values. They still visually, that is, on the handset screen, report their values from before the rotation, but to the code report their default values.

An example, I have an activity with an EditText, I enter a number, 15, the calculator starts calculating, displays a result. If at this point I rotate to a different orientation, as expected the activity dies, reloads, shows the new orientation, my value of 15 is still in the EditText, however the calculator (the code) calculates as if it was 0, which is the initial value. I have not tried changing the initial value to see if it is really pulling from the default value, or just setting itself to 0 when I cast it as an int.

If I didn't explain this well enough, let me know, I can add source code if I need to.
Thanks.

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

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

发布评论

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

评论(1

一个人的夜不怕黑 2024-10-19 09:09:47

您什么时候读取这些值?视图状态由 ActivityonRestoreInstanceState 默认实现保留,该实现发生在 onStartonPostCreate 之间。如果您在发生这种情况之前读取这些值,那就可以解释为什么您看到的是计算的默认值,而不是绘制屏幕后显示的值。

When are you reading the values? View state is preserved by Activity's default implementation of onRestoreInstanceState, which happens between onStart and onPostCreate. If you are reading the values before this happens, that would explain why you are seeing default values for your calculation rather than the values that are displayed once the screen is drawn.

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