1212import com .contrarywind .view .WheelView ;
1313
1414import java .text .DateFormat ;
15+ import java .text .ParseException ;
1516import java .text .SimpleDateFormat ;
1617import java .util .Arrays ;
1718import java .util .Calendar ;
19+ import java .util .Date ;
1820import java .util .List ;
1921
2022
@@ -37,13 +39,27 @@ public class WheelTime {
3739 private static final int DEFAULT_START_DAY = 1 ;
3840 private static final int DEFAULT_END_DAY = 31 ;
3941
42+ private static final int DEFAULT_END_HOUR = 23 ;
43+ private static final int DEFAULT_END_MINUTE = 59 ;
44+ private static final int DEFAULT_END_SECOND = 59 ;
45+
4046 private int startYear = DEFAULT_START_YEAR ;
4147 private int endYear = DEFAULT_END_YEAR ;
4248 private int startMonth = DEFAULT_START_MONTH ;
4349 private int endMonth = DEFAULT_END_MONTH ;
4450 private int startDay = DEFAULT_START_DAY ;
4551 private int endDay = DEFAULT_END_DAY ; //表示31天的
52+
53+ private int endHour = DEFAULT_END_HOUR ;
54+ private int endMinute = DEFAULT_END_MINUTE ;
55+ private int endSecond = DEFAULT_END_SECOND ;
56+
4657 private int currentYear ;
58+ private int currentMonth ;
59+ private int currentDay ;
60+ private int currentHour ;
61+ private int currentMinute ;
62+ private int currentSecond ;
4763
4864 private int textSize ;
4965
@@ -246,6 +262,11 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) {
246262 final List <String > list_little = Arrays .asList (months_little );
247263
248264 currentYear = year ;
265+ currentMonth = month + 1 ;
266+ currentDay = day ;
267+ currentHour = h ;
268+ currentMinute = m ;
269+ currentSecond = s ;
249270 // 年
250271 wv_year = (WheelView ) view .findViewById (R .id .year );
251272 wv_year .setAdapter (new NumericWheelAdapter (startYear , endYear ));// 设置"年"的显示数据
@@ -357,19 +378,23 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) {
357378 wv_day .setGravity (gravity );
358379 //时
359380 wv_hours = (WheelView ) view .findViewById (R .id .hour );
360- wv_hours .setAdapter (new NumericWheelAdapter (0 , 23 ));
381+ if (year < endYear || month + 1 < endMonth || day < endDay || h < endHour ) {
382+ wv_hours .setAdapter (new NumericWheelAdapter (0 , DEFAULT_END_HOUR ));
383+ } else {
384+ wv_hours .setAdapter (new NumericWheelAdapter (0 , endHour ));
385+ }
361386
362387 wv_hours .setCurrentItem (h );
363388 wv_hours .setGravity (gravity );
364389 //分
365390 wv_minutes = (WheelView ) view .findViewById (R .id .min );
366- wv_minutes .setAdapter (new NumericWheelAdapter (0 , 59 ));
391+ wv_minutes .setAdapter (new NumericWheelAdapter (0 , endMinute ));
367392
368393 wv_minutes .setCurrentItem (m );
369394 wv_minutes .setGravity (gravity );
370395 //秒
371396 wv_seconds = (WheelView ) view .findViewById (R .id .second );
372- wv_seconds .setAdapter (new NumericWheelAdapter (0 , 59 ));
397+ wv_seconds .setAdapter (new NumericWheelAdapter (0 , endSecond ));
373398
374399 wv_seconds .setCurrentItem (s );
375400 wv_seconds .setGravity (gravity );
@@ -519,15 +544,22 @@ public void onItemSelected(int index) {
519544 setContentTextSize ();
520545 }
521546
522- private void setChangedListener (WheelView wheelView ) {
523- if (mSelectChangeCallback != null ) {
524- wheelView .setOnItemSelectedListener (new OnItemSelectedListener () {
525- @ Override
526- public void onItemSelected (int index ) {
547+ private void setChangedListener (final WheelView wheelView ) {
548+ wheelView .setOnItemSelectedListener (new OnItemSelectedListener () {
549+ @ Override
550+ public void onItemSelected (int index ) {
551+ if (wheelView == wv_day ) {
552+ setReHour ();
553+ } else if (wheelView == wv_hours ) {
554+ setReMinute ();
555+ } else if (wheelView == wv_minutes ) {
556+ setReSecond ();
557+ }
558+ if (mSelectChangeCallback != null ) {
527559 mSelectChangeCallback .onTimeSelectChanged ();
528560 }
529- });
530- }
561+ }
562+ });
531563
532564 }
533565
@@ -541,12 +573,14 @@ private void setReDay(int year_num, int monthNum, int startD, int endD, List<Str
541573 endD = 31 ;
542574 }
543575 wv_day .setAdapter (new NumericWheelAdapter (startD , endD ));
576+ setReHour ();
544577// maxItem = endD;
545578 } else if (list_little .contains (String .valueOf (monthNum ))) {
546579 if (endD > 30 ) {
547580 endD = 30 ;
548581 }
549582 wv_day .setAdapter (new NumericWheelAdapter (startD , endD ));
583+ setReHour ();
550584// maxItem = endD;
551585 } else {
552586 if ((year_num % 4 == 0 && year_num % 100 != 0 )
@@ -555,12 +589,14 @@ private void setReDay(int year_num, int monthNum, int startD, int endD, List<Str
555589 endD = 29 ;
556590 }
557591 wv_day .setAdapter (new NumericWheelAdapter (startD , endD ));
592+ setReHour ();
558593// maxItem = endD;
559594 } else {
560595 if (endD > 28 ) {
561596 endD = 28 ;
562597 }
563598 wv_day .setAdapter (new NumericWheelAdapter (startD , endD ));
599+ setReHour ();
564600// maxItem = endD;
565601 }
566602 }
@@ -571,6 +607,70 @@ private void setReDay(int year_num, int monthNum, int startD, int endD, List<Str
571607 }
572608 }
573609
610+ private void setReHour () {
611+ try {
612+ Date date = WheelTime .dateFormat .parse (getTime ());
613+ Calendar calendar = Calendar .getInstance ();
614+ calendar .setTime (date );
615+ int year = calendar .get (Calendar .YEAR );
616+ int month = calendar .get (Calendar .MONTH ) + 1 ;
617+ int day = calendar .get (Calendar .DAY_OF_MONTH );
618+
619+ if (year < endYear || month < endMonth || day < endDay ) {
620+ wv_hours .setAdapter (new NumericWheelAdapter (0 , DEFAULT_END_HOUR ));
621+ } else {
622+ wv_hours .setAdapter (new NumericWheelAdapter (0 , endHour ));
623+ wv_hours .setCurrentItem (wv_hours .getMaxValue ());
624+ }
625+ setReMinute ();
626+ } catch (ParseException e ) {
627+ e .printStackTrace ();
628+ }
629+ }
630+
631+ private void setReMinute () {
632+ try {
633+ Date date = WheelTime .dateFormat .parse (getTime ());
634+ Calendar calendar = Calendar .getInstance ();
635+ calendar .setTime (date );
636+ int year = calendar .get (Calendar .YEAR );
637+ int month = calendar .get (Calendar .MONTH ) + 1 ;
638+ int day = calendar .get (Calendar .DAY_OF_MONTH );
639+ int h = calendar .get (Calendar .HOUR_OF_DAY );
640+
641+ if (year < endYear || month < endMonth || day < endDay || h < endHour ) {
642+ wv_minutes .setAdapter (new NumericWheelAdapter (0 , DEFAULT_END_MINUTE ));
643+ } else {
644+ wv_minutes .setAdapter (new NumericWheelAdapter (0 , endMinute ));
645+ wv_minutes .setCurrentItem (wv_minutes .getMaxValue ());
646+ }
647+ setReSecond ();
648+ } catch (ParseException e ) {
649+ e .printStackTrace ();
650+ }
651+ }
652+
653+ private void setReSecond () {
654+ try {
655+ Date date = WheelTime .dateFormat .parse (getTime ());
656+ Calendar calendar = Calendar .getInstance ();
657+ calendar .setTime (date );
658+ int year = calendar .get (Calendar .YEAR );
659+ int month = calendar .get (Calendar .MONTH ) + 1 ;
660+ int day = calendar .get (Calendar .DAY_OF_MONTH );
661+ int h = calendar .get (Calendar .HOUR_OF_DAY );
662+ int m = calendar .get (Calendar .MINUTE );
663+ if (year < endYear || month < endMonth || day < endDay || h < endHour || m < endMinute ) {
664+ wv_seconds .setAdapter (new NumericWheelAdapter (0 , DEFAULT_END_SECOND ));
665+ } else {
666+ wv_seconds .setAdapter (new NumericWheelAdapter (0 , endSecond ));
667+ wv_seconds .setCurrentItem (wv_seconds .getMaxValue ());
668+ }
669+ } catch (ParseException e ) {
670+ e .printStackTrace ();
671+ }
672+ }
673+
574674
575675 private void setContentTextSize () {
576676 wv_day .setTextSize (textSize );
@@ -743,20 +843,32 @@ public void setRangDate(Calendar startDate, Calendar endDate) {
743843 int year = endDate .get (Calendar .YEAR );
744844 int month = endDate .get (Calendar .MONTH ) + 1 ;
745845 int day = endDate .get (Calendar .DAY_OF_MONTH );
846+ int hour = endDate .get (Calendar .HOUR_OF_DAY );
847+ int minute = endDate .get (Calendar .MINUTE );
848+ int second = endDate .get (Calendar .SECOND );
746849 if (year > startYear ) {
747850 this .endYear = year ;
748851 this .endMonth = month ;
749852 this .endDay = day ;
853+ this .endHour = hour ;
854+ this .endMinute = minute ;
855+ this .endSecond = second ;
750856 } else if (year == startYear ) {
751857 if (month > startMonth ) {
752858 this .endYear = year ;
753859 this .endMonth = month ;
754860 this .endDay = day ;
861+ this .endHour = hour ;
862+ this .endMinute = minute ;
863+ this .endSecond = second ;
755864 } else if (month == startMonth ) {
756865 if (day > startDay ) {
757866 this .endYear = year ;
758867 this .endMonth = month ;
759868 this .endDay = day ;
869+ this .endHour = hour ;
870+ this .endMinute = minute ;
871+ this .endSecond = second ;
760872 }
761873 }
762874 }
0 commit comments