Skip to content

Commit 22de447

Browse files
author
杨宏伟
committed
增加起始时间时分秒范围控制
1 parent 2645e6d commit 22de447

File tree

3 files changed

+129
-21
lines changed

3 files changed

+129
-21
lines changed

app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void onClick(View v) {
9898
/* pvTime.show(); //show timePicker*/
9999
initTimePicker();
100100
Calendar date = Calendar.getInstance();
101-
date.set(2021, 8, 15, 15, 40, 30);
101+
// date.set(2021, 8, 15, 15, 40, 30);
102102
pvTime.setDate(date);
103103
pvTime.show(v);//弹出时间选择器,传递参数过去,回调的时候则可以绑定此view
104104
} else if (v.getId() == R.id.btn_Options && pvOptions != null) {
@@ -200,8 +200,8 @@ private void setTimePickerChildWeight(View v, float yearWeight, float weight) {
200200
private void initTimePicker() {//Dialog 模式下,在底部弹出
201201
Calendar startDate = Calendar.getInstance();
202202
Calendar endDate = Calendar.getInstance();
203-
startDate.set(2019, 1, 23, 12, 10, 30);
204-
endDate.set(2021, 1, 23, 12, 10, 30);
203+
startDate.set(2021, 3, 23, 12, 10, 30);
204+
endDate.set(2022, 9, 24, 12, 10, 30);
205205
pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
206206
@Override
207207
public void onTimeSelect(Date date, View v) {
@@ -228,7 +228,7 @@ public void onClick(View view) {
228228
.setLineSpacingMultiplier(2.0f)
229229
.isAlphaGradient(true)
230230
// .setDate(startDate)
231-
.setRangDate(startDate, Calendar.getInstance())
231+
.setRangDate(startDate, endDate)
232232
.build();
233233

234234
Dialog mDialog = pvTime.getDialog();

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.content.Context;
44
import android.text.TextUtils;
5+
import android.util.Log;
56
import android.view.LayoutInflater;
67
import android.view.View;
78
import android.widget.Button;
@@ -14,6 +15,7 @@
1415
import com.bigkoo.pickerview.listener.ISelectTimeCallback;
1516

1617
import java.text.ParseException;
18+
import java.text.SimpleDateFormat;
1719
import java.util.Calendar;
1820
import java.util.Date;
1921

@@ -193,6 +195,35 @@ private void initDefaultSelectedDate() {
193195
private void setTime() {
194196
int year, month, day, hours, minute, seconds;
195197
Calendar calendar = Calendar.getInstance();
198+
Log.i("TAG", "===> " + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime()));
199+
boolean isInRange = true;
200+
if (mPickerOptions.startDate != null && mPickerOptions.endDate != null) {
201+
if (calendar.getTimeInMillis() < mPickerOptions.startDate.getTimeInMillis()
202+
|| calendar.getTimeInMillis() > mPickerOptions.endDate.getTimeInMillis()) {
203+
isInRange = false;
204+
calendar = mPickerOptions.endDate;
205+
}
206+
} else if (mPickerOptions.startDate != null) {
207+
if (calendar.getTimeInMillis() < mPickerOptions.startDate.getTimeInMillis()) {
208+
isInRange = false;
209+
calendar = mPickerOptions.endDate;
210+
}
211+
} else if (mPickerOptions.endDate != null) {
212+
if (calendar.getTimeInMillis() > mPickerOptions.endDate.getTimeInMillis()) {
213+
isInRange = false;
214+
calendar = mPickerOptions.endDate;
215+
}
216+
}
217+
if (!isInRange) {
218+
year = calendar.get(Calendar.YEAR);
219+
month = calendar.get(Calendar.MONTH);
220+
day = calendar.get(Calendar.DAY_OF_MONTH);
221+
hours = calendar.get(Calendar.HOUR_OF_DAY);
222+
minute = calendar.get(Calendar.MINUTE);
223+
seconds = calendar.get(Calendar.SECOND);
224+
wheelTime.setPicker(year, month, day, hours, minute, seconds);
225+
return;
226+
}
196227

197228
if (mPickerOptions.date == null) {
198229
calendar.setTimeInMillis(System.currentTimeMillis());

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)