Android datepicker不记得最后选择的日期

时间:2016-04-28 15:48:18

标签: android datepicker android-datepicker

我设法在我的应用程序中创建了一个datepicker对话框,但我遇到了问题。当我在datepicker对话框上选择日期时,它不记得该日期。所以,如果我再次打开该对话框,它会显示我为第一个打开的系统日期。在这件事上有人可以帮助我吗?

我也附上了我的代码:

public void showDateDialog() {

    final Dialog dialog = new Dialog(this);

    dialog.setContentView(R.layout.date_time_layout);

    dialog.setTitle(getResources().getString(R.string.t_select_date));

    final DatePicker dp = (DatePicker) dialog
            .findViewById(R.id.datePicker1);
    Button btnDismissPicker = (Button) dialog
            .findViewById(R.id.btnDismissPicker);
    Button btnOkDismiss = (Button) dialog.findViewById(R.id.btnOkPicker);

    dp.init(dp.getYear(), dp.getMonth(), dp.getDayOfMonth(),
            new OnDateChangedListener() {

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

                }
            });

    btnDismissPicker.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    btnOkDismiss.setOnClickListener(new OnClickListener() {

        @SuppressLint("ResourceAsColor")
        @Override
        public void onClick(View v) {

            btndate.setText(DateUtils.getDatePickerDate(dp));
            selectedDate = DateUtils.getTimeStmapfromDate(btndate.getText()
                    .toString()) + "";
            Altereddate = "1";
            dialog.dismiss();
        }
    });
    dialog.show();
}

1 个答案:

答案 0 :(得分:0)

 if (datePickerDialog == null) {
        Calendar calendar = Calendar.getInstance();
        datePickerDialog = new DatePickerDialog(getContext(), this, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
        datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
 }
 datePickerDialog.show();


  @Override
 public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
    .....
    datePickerDialog.updateDate(year, monthOfYear, dayOfMonth);
    ......
}