如何在下次通话时在DatePickerDialog中显示先前选择的日期? [英] How do I show the previously selected date in a DatePickerDialog on next call?

查看:71
本文介绍了如何在下次通话时在DatePickerDialog中显示先前选择的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DatePickerDialog加载到一个Fragment中,当第一次打开该对话框时,今天的显示为默认日期.然后,用户选择一个日期,然后关闭该对话框.在下一次重新打开时,如何显示以前选择的日期作为默认日期,而不是显示今天的日期?

A DatePickerDialog is loaded in a Fragment with today's showing as the default date when the dialog is first opened. The user then selects a date and then the dialog is dismissed. On next re-open, how do I show the previously selected date as the default date rather than it showing today's date?

我在Activity中尝试了.set(),但是不知道如何使用.get()来检索片段中的日期.我已经尝试过在Activity和片段之间使用侦听器进行接口,但是无法使其正常工作,对于我要实现的简单任务而言,它似乎过于复杂.而且我尝试了从Activity发送到片段的捆绑包,但是我也无法使其正常工作.请告知.

I have tried .set() in the Activity but don't know how to use .get() to retrieve the date in the fragment. I've tried an interface with a listener between the Activity and the fragement but could not get that to work and it seems overly complicated for the simple task I am trying to achieve. And I've tried a bundle sent from the Activity to the fragment but I haven't been able to get that to work either. Please advise.

活动文件:

...
final Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR, year);
c.set(Calendar.MONTH, month);
c.set(Calendar.DAY_OF_MONTH, day);
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");

片段文件:

...
public static class DatePickerFragment extends DialogFragment
                        implements DatePickerDialog.OnDateSetListener {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    return new DatePickerDialog(getActivity(), this, year, month, day);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    // Save selected date in a date variable here?  How?
    // How do I then use the date variable in onCreateDialog
    // rather than the get(Calendar) code that returns today's date? 
}

}

推荐答案

全局声明

private DatePicker datepicker;

复制并粘贴以下功能

Copy and paste below function

   private void selectDate() {

    final DatePickerDialog datePickerDialog = new DatePickerDialog(getActivity(),
            new DatePickerDialog.OnDateSetListener() {

                @Override
                public void onDateSet(DatePicker view, int year,
                                      int monthOfYear, int dayOfMonth) {

                    datepicker = new DatePicker(getActivity());

                    datepicker.init(year, monthOfYear + 1, dayOfMonth, null);


                }
            }, mYear, mMonth, mDay);

    if (datepicker != null) {
        datePickerDialog.updateDate(datepicker.getYear(), datepicker.getMonth() - 1, datepicker.getDayOfMonth());

    }
    datePickerDialog.show();
}

这篇关于如何在下次通话时在DatePickerDialog中显示先前选择的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆