Skip to content

Commit ed36427

Browse files
author
杨宏伟
committed
增加时分秒范围控制
1 parent 48bb91d commit ed36427

File tree

9 files changed

+173
-22
lines changed

9 files changed

+173
-22
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ protected void onCreate(Bundle savedInstanceState) {
6363
getOptionData();
6464

6565
initTimePicker();
66-
initCustomTimePicker();
66+
/* initCustomTimePicker();
6767
initLunarPicker();
6868
initOptionPicker();
6969
initCustomOptionPicker();
70-
initNoLinkOptionsPicker();
70+
initNoLinkOptionsPicker();*/
7171

7272
Button btn_Time = (Button) findViewById(R.id.btn_Time);
7373
btn_Options = (Button) findViewById(R.id.btn_Options);
@@ -194,6 +194,8 @@ private void setTimePickerChildWeight(View v, float yearWeight, float weight) {
194194

195195

196196
private void initTimePicker() {//Dialog 模式下,在底部弹出
197+
Calendar startDate = Calendar.getInstance();
198+
startDate.set(2000, 1, 23, 12, 10, 30);
197199
pvTime = new TimePickerBuilder(this, new OnTimeSelectListener() {
198200
@Override
199201
public void onTimeSelect(Date date, View v) {
@@ -208,7 +210,7 @@ public void onTimeSelectChanged(Date date) {
208210
Log.i("pvTime", "onTimeSelectChanged");
209211
}
210212
})
211-
.setType(new boolean[]{true, true, true, true, true, true})
213+
.setType(new boolean[]{true, true, true, true, true, false})
212214
.isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。
213215
.addOnCancelClickListener(new View.OnClickListener() {
214216
@Override
@@ -219,6 +221,7 @@ public void onClick(View view) {
219221
.setItemVisibleCount(5) //若设置偶数,实际值会加1(比如设置6,则最大可见条目为7)
220222
.setLineSpacingMultiplier(2.0f)
221223
.isAlphaGradient(true)
224+
.setRangDate(startDate, Calendar.getInstance())
222225
.build();
223226

224227
Dialog mDialog = pvTime.getDialog();

build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
buildscript {
44
repositories {
55
maven {
6-
url 'https://maven.google.com/'
7-
name 'Google'
6+
url 'http://maven.aliyun.com/nexus/content/groups/public/'
87
}
8+
google()
99
jcenter()
1010
}
1111
dependencies {
@@ -19,11 +19,11 @@ buildscript {
1919

2020
allprojects {
2121
repositories {
22-
jcenter()
2322
maven {
24-
url 'https://maven.google.com/'
25-
name 'Google'
23+
url 'http://maven.aliyun.com/nexus/content/groups/public/'
2624
}
25+
google()
26+
jcenter()
2727
}
2828
}
2929

gradle/wrapper/gradle-wrapper.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip

pickerview/src/main/java/com/bigkoo/pickerview/adapter/ArrayWheelAdapter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,14 @@ public int indexOf(Object o){
4141
return items.indexOf(o);
4242
}
4343

44+
@Override
45+
public int getMaxValue() {
46+
return 0;
47+
}
48+
49+
@Override
50+
public int getMinValue() {
51+
return 0;
52+
}
53+
4454
}

pickerview/src/main/java/com/bigkoo/pickerview/adapter/NumericWheelAdapter.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,14 @@ public int indexOf(Object o){
4444
}
4545

4646
}
47+
48+
@Override
49+
public int getMaxValue() {
50+
return maxValue;
51+
}
52+
53+
@Override
54+
public int getMinValue() {
55+
return minValue;
56+
}
4757
}

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

Lines changed: 122 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import com.contrarywind.view.WheelView;
1313

1414
import java.text.DateFormat;
15+
import java.text.ParseException;
1516
import java.text.SimpleDateFormat;
1617
import java.util.Arrays;
1718
import java.util.Calendar;
19+
import java.util.Date;
1820
import 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
}

pickerview/src/main/res/layout/include_pickerview_topbar.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
android:layout_width="match_parent"
2323
android:layout_height="wrap_content"
2424
android:layout_centerVertical="true"
25-
android:layout_toLeftOf="@+id/btnSubmit"
26-
android:layout_toRightOf="@+id/btnCancel"
25+
android:layout_toLeftOf="@id/btnSubmit"
26+
android:layout_toRightOf="@id/btnCancel"
2727
android:gravity="center"
2828
android:textColor="@color/pickerview_topbar_title"
2929
android:textSize="@dimen/pickerview_topbar_title_textsize" />
3030

3131
<Button
32-
android:id="@id/btnSubmit"
32+
android:id="@+id/btnSubmit"
3333
android:layout_width="wrap_content"
3434
android:layout_height="match_parent"
3535
android:layout_alignParentRight="true"

wheelview/src/main/java/com/contrarywind/adapter/WheelAdapter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ public interface WheelAdapter<T> {
2222
* @return the maximum item length or -1
2323
*/
2424
int indexOf(T o);
25+
26+
int getMaxValue();
27+
28+
int getMinValue();
2529
}

0 commit comments

Comments
 (0)