想要在datepicker上选择上一个日期。 (机器人)

时间:2014-07-07 10:57:40

标签: java android datepicker timepicker

我在自定义对话框中有一个日期选择器和一个时间选择器。如果我更改日期选择器上的日期,它将在Textview上更新。但是当我再次选择日期选择器时,它会给出今天的日期。我希望在datepicker上显示之前选择的日期。 这是我的代码。请帮助!!

from.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            picker = new Dialog(getActivity());
            picker.setContentView(R.layout.datetime);
            picker.setTitle("Select Date and Time");
            datep = (DatePicker) picker.findViewById(R.id.datePicker);
            timep = (TimePicker) picker.findViewById(R.id.timePicker1);

            set = (Button) picker.findViewById(R.id.btnSet);
            set.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub
                    month = datep.getMonth() + 1;
                    day = datep.getDayOfMonth();
                    year = datep.getYear();
                    hour = timep.getCurrentHour();
                    minute = timep.getCurrentMinute();
                    from.setText(year + "-" + month + "-" + day + " "
                            + hour + ":" + minute);
                    picker.dismiss();



                }
            });
            picker.show();
        }
    });    

3 个答案:

答案 0 :(得分:2)

找到答案。只需按下显示按钮的按钮的范围即可...

    picker = new Dialog(getActivity());
    picker.setContentView(R.layout.datetime);
    picker.setTitle("Select Date and Time");
    datep = (DatePicker) picker.findViewById(R.id.datePicker);
    timep = (TimePicker) picker.findViewById(R.id.timePicker1);


from.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            set = (Button) picker.findViewById(R.id.btnSet);
            set.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    // TODO Auto-generated method stub


                    //datep.updateDate(year, month, day);

                    month = datep.getMonth() + 1;
                    day = datep.getDayOfMonth();
                    year = datep.getYear();
                    hour = timep.getCurrentHour();
                    minute = timep.getCurrentMinute();
                    from.setText(year + "-" + month + "-" + day + " "
                            + hour + ":" + minute);
                    picker.dismiss();



                }
            });
            picker.show();
        }
    });

答案 1 :(得分:0)

您有两个选择:

1)在变量中保存先前选定的日,月和年 - 并将它们设置为DatePickerDialog

2)从文本中获取保存的日期 - 并将它们设置为DatePickerDialog

答案 2 :(得分:0)

您需要在DatePicker对话框上添加一个OnDateSetListener,如下所示:

/**
 * Initialize DatePickerDialog
 */
private void initDatePicker() {
    myCalendar = Calendar.getInstance();

    date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            myCalendar.set(Calendar.YEAR, year);
            myCalendar.set(Calendar.MONTH, monthOfYear);
            myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            updateLabel();
        }
    };

    dateDialog = new DatePickerDialog(getActivity(), date, 
            myCalendar.get(Calendar.YEAR), 
            myCalendar.get(Calendar.MONTH),
            myCalendar.get(Calendar.DAY_OF_MONTH));

    dateDialog.getDatePicker().setMaxDate(new Date().getTime());
}

初始化此方法后,它会在设置后自动在日期选择器上显示日期。 (: