限制时间选择器的时间范围

时间:2015-09-19 10:02:32

标签: java android jodatime datetimepicker android-timepicker

如何控制TimePicker,我只想在08:00 am到02:00 pm之间显示时间范围(从上午08:00到下午02:00隐藏全部)

我已经编写了完整的代码来显示TimePicker和自定义时间。

这是我的代码:

         case DIALOG_TIME:
            final Calendar c = Calendar.getInstance();
            int hour = c.get(Calendar.HOUR_OF_DAY);
            int minute = c.get(Calendar.MINUTE);

            // Create a new instance of TimePickerDialog and return it
            return new TimePickerDialog(this, lisTime, hour, minute,
            DateFormat.is24HourFormat(MainActivity.this));

        TimePickerDialog.OnTimeSetListener lisTime = new TimePickerDialog.OnTimeSetListener() {

        @Override
        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            String meridiem = "";
            nim = minute;
            Calendar datetime = Calendar.getInstance();
            datetime.set(Calendar.HOUR_OF_DAY, hourOfDay);
            datetime.set(Calendar.MINUTE, minute);

            if (datetime.get(Calendar.AM_PM) == Calendar.AM)
                meridiem = "AM";
            else if (datetime.get(Calendar.AM_PM) == Calendar.PM)
                meridiem = "PM";

            hour = (datetime.get(Calendar.HOUR) == 0) ?"12":String.valueOf(datetime.get(Calendar.HOUR));

            String time = pad(Integer.parseInt(hour)) + ":" + pad(minute) + " " + meridiem;
            editTime.setText(time);

        }
    };

    private String pad(int value){

        if(value<10){
            return "0"+value;
        }

        return ""+value;
    }

1 个答案:

答案 0 :(得分:2)

在答案中:https://stackoverflow.com/a/20396673/4130107 您可以创建自定义TimePickerDialog并覆盖onAttachedToWindow();而是限制小时范围:

[ng\:cloak], [ng-cloak], [data-ng-cloak], [x-ng-cloak], .ng-cloak, .x-ng-cloak
{
 display: none !important;
}

编辑:现在也为AM / PM工作

enter image description here