2

我有时间选择器,我想动态设置时间选择器的最小值和最大值例如:获取当前时间加上 2:30 小时作为最小值,最大值是晚上 11:00。

时间选择器对话框

public class Time_picker extends DialogFragment{

OnTimeSetListener ontimeSet;

int minHour;
int minMinute;

private int maxHour = 24;
private int maxMinute =00;
DateFormat dateFormat;
public Time_picker() {

}

public void setCallBack(OnTimeSetListener ontime) {

    ontimeSet = ontime;

}

private int hours, minutes;
@Override
public void setArguments(Bundle args) {
    super.setArguments(args);
    hours = args.getInt("hours");
    minutes = args.getInt("minute");

    minHour = hours;//current time plus 2:30hrs
    minMinute = -1;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    TimePickerDialog _time = new TimePickerDialog(getActivity(), ontimeSet, hours, minutes,false){

        @Override
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub

            try{
                boolean validTime;
                if(hourOfDay < minHour) {
                    validTime = false;
                }
                else if(hourOfDay == minHour) {
                    validTime = minute >= minMinute;
                }
                else if(hourOfDay == maxHour) {
                    validTime = minute <= maxMinute;
                }
                else {
                    validTime = true;
                }

                if(validTime) {
                    hours = hourOfDay;
                    minutes = minute;
                }
                else {
                    updateTime(hours, minutes);
                }

            }catch(Exception e){

            }
        }
    };
    return _time;
}
}

有时它正确显示加上 2:30 小时,但到时候它显示 1 小时后选择器。我无法找出问题。任何人都可以帮助我解决这个问题。

4

0 回答 0