简体   繁体   English

如何设置TimePickerDialog的范围?

[英]How to set a range for TimePickerDialog?

例如,我希望TimePicker仅显示当前时间之后或接下来的12小时内的时间,该怎么做?

Look this example.In this peace of code, I am controlling the max and min date. 看这个例子,在这段代码中,我控制着最大和最小日期。 Maybe you can modify this code to control the max and min time (not date). 也许您可以修改此代码以控制最大和最小时间(而不是日期)。 I think you are not going to have too many problems modifying this code. 我认为修改该代码不会有太多问题。

For Android version above to Honeycom we can use the method setMinDate(). 对于Honeycom以上的Android版本,我们可以使用setMinDate()方法。 For older versions we control the max and min when the user try to modify the date. 对于较旧的版本,当用户尝试修改日期时,我们控制最大值和最小值。

// Calendar
this.calendar = new GregorianCalendar();
this.datePicker = (DatePicker) findViewById(R.id.xxxxx);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    // (picker is a DatePicker)
    this.datePicker.setMinDate(this.calendar.getTimeInMillis());
} else {
    final int minYear = this.calendar.get(Calendar.YEAR);
    final int minMonth = this.calendar.get(Calendar.MONTH);
    final int minDay = this.calendar.get(Calendar.DAY_OF_MONTH);

    this.datePicker.init(minYear, minMonth, minDay,
        new OnDateChangedListener() {

            public void onDateChanged(DatePicker view, int year,
                    int month, int day) {
                Calendar newDate = Calendar.getInstance();
                newDate.set(year, month, day);

                if (calendar.after(newDate)) {
                    view.init(minYear, minMonth, minDay, this);
                }
            }
        });
   Log.w(TAG, "API Level < 11 so not restricting date range...");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM