Skip to content

Commit e4e0696

Browse files
authored
增加起始时间时分秒控制
1 parent 2645e6d commit e4e0696

File tree

1 file changed

+94
-17
lines changed

1 file changed

+94
-17
lines changed

pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java

Lines changed: 94 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public class WheelTime {
4040
private static final int DEFAULT_START_DAY = 1;
4141
private static final int DEFAULT_END_DAY = 31;
4242

43+
private static final int DEFAULT_START_HOUR = 0;
44+
private static final int DEFAULT_START_MINUTE = 0;
45+
private static final int DEFAULT_START_SECOND = 0;
46+
4347
private static final int DEFAULT_END_HOUR = 23;
4448
private static final int DEFAULT_END_MINUTE = 59;
4549
private static final int DEFAULT_END_SECOND = 59;
@@ -51,6 +55,10 @@ public class WheelTime {
5155
private int startDay = DEFAULT_START_DAY;
5256
private int endDay = DEFAULT_END_DAY; //表示31天的
5357

58+
private int startHour = DEFAULT_START_HOUR;
59+
private int startMinute = DEFAULT_START_MINUTE;
60+
private int startSecond = DEFAULT_START_SECOND;
61+
5462
private int endHour = DEFAULT_END_HOUR;
5563
private int endMinute = DEFAULT_END_MINUTE;
5664
private int endSecond = DEFAULT_END_SECOND;
@@ -379,31 +387,55 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) {
379387
wv_day.setGravity(gravity);
380388
//时
381389
wv_hours = (WheelView) view.findViewById(R.id.hour);
390+
int tempStartHour;
391+
int tempEndHour;
392+
if (year > startYear || month + 1 > startMonth || day > startDay) {
393+
tempStartHour = DEFAULT_START_HOUR;
394+
} else {
395+
tempStartHour = startHour;
396+
}
382397
if (year < endYear || month + 1 < endMonth || day < endDay) {
383-
wv_hours.setAdapter(new NumericWheelAdapter(0, DEFAULT_END_HOUR));
398+
tempEndHour = DEFAULT_END_HOUR;
384399
} else {
385-
wv_hours.setAdapter(new NumericWheelAdapter(0, endHour));
400+
tempEndHour = endHour;
386401
}
402+
wv_hours.setAdapter(new NumericWheelAdapter(tempStartHour, tempEndHour));
387403

388404
wv_hours.setCurrentItem(h);
389405
wv_hours.setGravity(gravity);
390406
//分
391407
wv_minutes = (WheelView) view.findViewById(R.id.min);
408+
int tempStartMinute;
409+
int tempEndMinute;
410+
if (year > startYear || month + 1 > startMonth || day > startDay || h > startHour) {
411+
tempStartMinute = DEFAULT_START_MINUTE;
412+
} else {
413+
tempStartMinute = startMinute;
414+
}
392415
if (year < endYear || month + 1 < endMonth || day < endDay || h < endHour) {
393-
wv_minutes.setAdapter(new NumericWheelAdapter(0, DEFAULT_END_SECOND));
416+
tempEndMinute = DEFAULT_END_MINUTE;
394417
} else {
395-
wv_minutes.setAdapter(new NumericWheelAdapter(0, endMinute));
418+
tempEndMinute = endMinute;
396419
}
420+
wv_minutes.setAdapter(new NumericWheelAdapter(tempStartMinute, tempEndMinute));
397421

398422
wv_minutes.setCurrentItem(m);
399423
wv_minutes.setGravity(gravity);
400424
//秒
401425
wv_seconds = (WheelView) view.findViewById(R.id.second);
426+
int tempStartSecond;
427+
int tempEndSecond;
428+
if (year > startYear || month + 1 > startMonth || day > startDay || h > startHour || m > startMinute) {
429+
tempStartSecond = DEFAULT_START_SECOND;
430+
} else {
431+
tempStartSecond = startSecond;
432+
}
402433
if (year < endYear || month + 1 < endMonth || day < endDay || h < endHour || m < endMinute) {
403-
wv_seconds.setAdapter(new NumericWheelAdapter(0, DEFAULT_END_SECOND));
434+
tempEndSecond = DEFAULT_END_SECOND;
404435
} else {
405-
wv_seconds.setAdapter(new NumericWheelAdapter(0, endSecond));
436+
tempEndSecond = endSecond;
406437
}
438+
wv_seconds.setAdapter(new NumericWheelAdapter(tempStartSecond, tempEndSecond));
407439

408440
wv_seconds.setCurrentItem(s);
409441
wv_seconds.setGravity(gravity);
@@ -626,11 +658,20 @@ private void setReHour() {
626658
int day = calendar.get(Calendar.DAY_OF_MONTH);
627659

628660
int position = wv_hours.getCurrentItem();
661+
int start;
662+
int end;
663+
if (year > startYear || month > startMonth || day > startDay) {
664+
start = DEFAULT_START_HOUR;
665+
} else {
666+
start = startHour;
667+
}
668+
629669
if (year < endYear || month < endMonth || day < endDay) {
630-
wv_hours.setAdapter(new NumericWheelAdapter(0, DEFAULT_END_HOUR));
670+
end = DEFAULT_END_HOUR;
631671
} else {
632-
wv_hours.setAdapter(new NumericWheelAdapter(0, endHour));
672+
end = endHour;
633673
}
674+
wv_hours.setAdapter(new NumericWheelAdapter(start, end));
634675

635676
wv_hours.setCurrentItem(Math.min(position, wv_hours.getItemsCount() - 1));
636677

@@ -649,13 +690,23 @@ private void setReMinute() {
649690
int month = calendar.get(Calendar.MONTH) + 1;
650691
int day = calendar.get(Calendar.DAY_OF_MONTH);
651692
int h = calendar.get(Calendar.HOUR_OF_DAY);
693+
Log.i("TAG===> ", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
652694

653695
int position = wv_minutes.getCurrentItem();
696+
int start;
697+
int end;
698+
if (year > startYear || month > startMonth || day > startDay || h > startHour) {
699+
start = DEFAULT_START_MINUTE;
700+
} else {
701+
start = startMinute;
702+
}
703+
654704
if (year < endYear || month < endMonth || day < endDay || h < endHour) {
655-
wv_minutes.setAdapter(new NumericWheelAdapter(0, DEFAULT_END_MINUTE));
705+
end = DEFAULT_END_MINUTE;
656706
} else {
657-
wv_minutes.setAdapter(new NumericWheelAdapter(0, endMinute));
707+
end = endMinute;
658708
}
709+
wv_minutes.setAdapter(new NumericWheelAdapter(start, end));
659710
wv_minutes.setCurrentItem(Math.min(position, wv_minutes.getItemsCount() - 1));
660711
setReSecond();
661712
} catch (ParseException e) {
@@ -674,11 +725,21 @@ private void setReSecond() {
674725
int h = calendar.get(Calendar.HOUR_OF_DAY);
675726
int m = calendar.get(Calendar.MINUTE);
676727
int position = wv_seconds.getCurrentItem();
728+
729+
int start;
730+
int end;
731+
if (year > startYear || month > startMonth || day > startDay || h > startHour || m > startMinute) {
732+
start = DEFAULT_START_SECOND;
733+
} else {
734+
start = startSecond;
735+
}
736+
677737
if (year < endYear || month < endMonth || day < endDay || h < endHour || m < endMinute) {
678-
wv_seconds.setAdapter(new NumericWheelAdapter(0, DEFAULT_END_SECOND));
738+
end = DEFAULT_END_SECOND;
679739
} else {
680-
wv_seconds.setAdapter(new NumericWheelAdapter(0, endSecond));
740+
end = endSecond;
681741
}
742+
wv_seconds.setAdapter(new NumericWheelAdapter(start, end));
682743
wv_seconds.setCurrentItem(Math.min(position, wv_seconds.getItemsCount() - 1));
683744
} catch (ParseException e) {
684745
e.printStackTrace();
@@ -771,9 +832,9 @@ public String getTime() {
771832
sb.append((wv_year.getCurrentItem() + startYear)).append("-")
772833
.append((wv_month.getCurrentItem() + startMonth)).append("-")
773834
.append((wv_day.getCurrentItem() + startDay)).append(" ")
774-
.append(wv_hours.getCurrentItem()).append(":")
775-
.append(wv_minutes.getCurrentItem()).append(":")
776-
.append(wv_seconds.getCurrentItem());
835+
.append(wv_hours.getCurrentItem() + startHour).append(":")
836+
.append(wv_minutes.getCurrentItem() + startMinute).append(":")
837+
.append(wv_seconds.getCurrentItem() + startSecond);
777838
} else {
778839
sb.append((wv_year.getCurrentItem() + startYear)).append("-")
779840
.append((wv_month.getCurrentItem() + startMonth)).append("-")
@@ -891,30 +952,46 @@ public void setRangDate(Calendar startDate, Calendar endDate) {
891952
int year = startDate.get(Calendar.YEAR);
892953
int month = startDate.get(Calendar.MONTH) + 1;
893954
int day = startDate.get(Calendar.DAY_OF_MONTH);
955+
int hour = startDate.get(Calendar.HOUR_OF_DAY);
956+
int minute = startDate.get(Calendar.MINUTE);
957+
int second = startDate.get(Calendar.SECOND);
894958
if (year < endYear) {
895959
this.startMonth = month;
896960
this.startDay = day;
897961
this.startYear = year;
962+
this.startHour = hour;
963+
this.startMinute = minute;
964+
this.startSecond = second;
898965
} else if (year == endYear) {
899966
if (month < endMonth) {
900967
this.startMonth = month;
901968
this.startDay = day;
902969
this.startYear = year;
970+
this.startHour = hour;
971+
this.startMinute = minute;
972+
this.startSecond = second;
903973
} else if (month == endMonth) {
904974
if (day < endDay) {
905975
this.startMonth = month;
906976
this.startDay = day;
907977
this.startYear = year;
978+
this.startHour = hour;
979+
this.startMinute = minute;
980+
this.startSecond = second;
908981
}
909982
}
910983
}
911984

912985
} else if (startDate != null && endDate != null) {
913986
this.startYear = startDate.get(Calendar.YEAR);
914-
this.endYear = endDate.get(Calendar.YEAR);
915987
this.startMonth = startDate.get(Calendar.MONTH) + 1;
916-
this.endMonth = endDate.get(Calendar.MONTH) + 1;
917988
this.startDay = startDate.get(Calendar.DAY_OF_MONTH);
989+
this.startHour = startDate.get(Calendar.HOUR_OF_DAY);
990+
this.startMinute = startDate.get(Calendar.MINUTE);
991+
this.startSecond = startDate.get(Calendar.SECOND);
992+
993+
this.endYear = endDate.get(Calendar.YEAR);
994+
this.endMonth = endDate.get(Calendar.MONTH) + 1;
918995
this.endDay = endDate.get(Calendar.DAY_OF_MONTH);
919996
this.endHour = endDate.get(Calendar.HOUR_OF_DAY);
920997
this.endMinute = endDate.get(Calendar.MINUTE);

0 commit comments

Comments
 (0)