如何将TimePickerDialog设置为当前时间并仅显示当前和未来时间而不显示以前的时间?

时间:2015-03-02 07:03:48

标签: android android-timepicker

我是Android的新手。我正在努力研究TimePickerDialog。我已经实现了它并且工作正常,但唯一的问题是它显示的是上一次。有没有办法只显示TimepickerDialog中的当前和未来时间?我做了以下编码,请指导我。

  TimePickerDialog mTimePicker;

        mTimePicker = new TimePickerDialog(Take_Away.this,
                new TimePickerDialog.OnTimeSetListener() {
                    @SuppressLint("SimpleDateFormat")
                    @Override
                    public void onTimeSet(TimePicker timePicker,
                            int selectedHour, int selectedMinute) {

                        Calendar time = Calendar.getInstance();

                        time.set(Calendar.HOUR_OF_DAY, selectedHour);

                        time.set(Calendar.MINUTE, selectedMinute);
                        SimpleDateFormat format = new SimpleDateFormat(
                                "hh:mm a");
                        et_time.setText(format.format(time.getTime()));
                        hour = selectedHour;
                        minute = selectedMinute;
                    }
                }, hour, minute, false);// Yes 24 hour time
        mTimePicker.setTitle("Select Time");
        mTimePicker.show();

4 个答案:

答案 0 :(得分:1)

在构建TimePickerDialog时传递当前日期的小时时间值。

// Get current system time
Calendar currentTime = Calendar.getInstance();

mTimePicker = new TimePickerDialog(Take_Away.this,
        new TimePickerDialog.OnTimeSetListener() {
            @SuppressLint("SimpleDateFormat")
            @Override
            public void onTimeSet(TimePicker timePicker,
                    int selectedHour, int selectedMinute) {

                Calendar time = Calendar.getInstance();

                time.set(Calendar.HOUR_OF_DAY, selectedHour);

                time.set(Calendar.MINUTE, selectedMinute);
                SimpleDateFormat format = new SimpleDateFormat(
                        "hh:mm a");
                et_time.setText(format.format(time.getTime()));
                hour = selectedHour;
                minute = selectedMinute;
            }
        },

        currentTime.get(Calendar.HOUR_OF_DAY), // Current hour value
        currentTime.get(Calendar.MINUTE), // Current minute value
        DateFormat.is24HourFormat()); // Check 24 Hour or AM/PM format

mTimePicker.setTitle("Select Time");
mTimePicker.show();

请注意DateFormat.is24HourFormat()的使用。这符合用户对24小时或AM / PM时间格式的系统范围偏好。

答案 1 :(得分:1)

使用setMinDate()函数。它设置自getDefault()时区自1970年1月1日00:00:00以来的最小日期(以毫秒为单位)。将最短日期设置为今天的日期,因此用户将无法通过它。像onCreateDialog中的后续内容:

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);

DatePickerDialog d = new DatePickerDialog(getActivity(), listener, year, month, day);  
DatePicker dp = d.getDatePicker(); 
dp.setMinDate(c.getTimeInMillis());
return d;

答案 2 :(得分:0)

尝试如下。希望它能帮到你

private DatePickerDialog.OnDateSetListener pickerListener = new DatePickerDialog.OnDateSetListener() {

        // when dialog box is closed, below method will be called.
        @Override
        public void onDateSet(DatePicker view, int selectedYear,
                int selectedMonth, int selectedDay) {

            year = selectedYear;
            month = selectedMonth;
            day = selectedDay;

            // Show selected date
            editOutputDate.setText(new StringBuilder().append(month + 1)
                    .append("-").append(day).append("-").append(year)
                    .append(" "));

        }`
    };

private int year;
private int month;
private int day;

答案 3 :(得分:0)

认为这会有所帮助 mTimePicker.updateTime(hourOfDay,minute);