如何在旋转屏幕后保持 DatePicKer 状态

发布于 2024-10-17 19:45:38 字数 292 浏览 2 评论 0原文

我已经实现了API提供的HelloDatePicker的Demo版本。

http://developer.android.com/resources/tutorials/views/hello -datepicker.html

但是,旋转屏幕后我无法保持其值。有人可以发布一个示例或说明如何做到这一点。

谢谢

I have implemented the Demo version of HelloDatePicker provided by the API.

http://developer.android.com/resources/tutorials/views/hello-datepicker.html

However I cannot maintain its value after rotating the screen. Can someone post an example or indicate how one can do this.

Thanks

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

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

发布评论

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

评论(3

意中人 2024-10-24 19:45:38

使用 onRetainNonConfigurationInstance() 保存日期,并在 onCreate 使用 getLastNonConfigurationInstance() 加载并设定日期。

Use onRetainNonConfigurationInstance() to save the date, and in onCreate use getLastNonConfigurationInstance() to load and set the date.

橘香 2024-10-24 19:45:38

即使没有 Fragment,单独的 Activity 也可以做到这一点:

@Override protected void onSaveInstanceState (Bundle outState)
{
    super.onSaveInstanceState (outState);
    outState.putInt ("YEAR", mDatePicker.getYear ());
    outState.putInt ("MONTH", mDatePicker.getMonth ());
    outState.putInt ("DAY", mDatePicker.getDayOfMonth ());
}

@Override protected void onRestoreInstanceState (Bundle savedInstanceState)
{
    super.onRestoreInstanceState (savedInstanceState);
    mDatePicker.updateDate (savedInstanceState.getInt ("YEAR"),
                            savedInstanceState.getInt ("MONTH"),
                            savedInstanceState.getInt ("DAY"));
}

Even without a Fragment, the Activity alone can do this:

@Override protected void onSaveInstanceState (Bundle outState)
{
    super.onSaveInstanceState (outState);
    outState.putInt ("YEAR", mDatePicker.getYear ());
    outState.putInt ("MONTH", mDatePicker.getMonth ());
    outState.putInt ("DAY", mDatePicker.getDayOfMonth ());
}

@Override protected void onRestoreInstanceState (Bundle savedInstanceState)
{
    super.onRestoreInstanceState (savedInstanceState);
    mDatePicker.updateDate (savedInstanceState.getInt ("YEAR"),
                            savedInstanceState.getInt ("MONTH"),
                            savedInstanceState.getInt ("DAY"));
}
逆光飞翔i 2024-10-24 19:45:38

轮换后,Activity 被重新创建。 添加:

android:configChanges="keyboardHidden|orientation"

避免这种情况的最简单方法是在 AndroidManifest.xml 的 Activity 声明中

有关更多信息您应该阅读它

After rotation, Activity is recreated. The simplest way to avoid it is to add:

android:configChanges="keyboardHidden|orientation"

in AndroidManifest.xml in Activity declaration.

For more information you should read it.

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