Skip to content

Commit 51f42ec

Browse files
committed
日期及时间选择器部分逻辑小修改
1 parent dbd9027 commit 51f42ec

5 files changed

Lines changed: 45 additions & 68 deletions

File tree

app/src/main/java/cn/qqtheme/androidpicker/MainActivity.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.alibaba.fastjson.JSON;
1212

1313
import java.util.ArrayList;
14-
import java.util.Calendar;
1514

1615
import cn.qqtheme.framework.picker.AddressPicker;
1716
import cn.qqtheme.framework.picker.ColorPicker;
@@ -28,8 +27,6 @@
2827

2928
public class MainActivity extends Activity {
3029

31-
private Calendar calendar = Calendar.getInstance();
32-
3330
@Override
3431
protected void onCreate(Bundle savedInstanceState) {
3532
super.onCreate(savedInstanceState);
@@ -78,8 +75,8 @@ public void onOptionPicked(int position, String option) {
7875
public void onYearMonthDayPicker(View view) {
7976
DatePicker picker = new DatePicker(this);
8077
picker.setRangeStart(2016, 8, 29);
81-
picker.setRangeEnd(2022, 1, 1);
82-
picker.setSelectedItem(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
78+
picker.setRangeEnd(2022, 1, 11);
79+
picker.setSelectedItem(2016, 10, 14);
8380
picker.setOnDatePickListener(new DatePicker.OnYearMonthDayPickListener() {
8481
@Override
8582
public void onDatePicked(String year, String month, String day) {
@@ -93,8 +90,6 @@ public void onDatePicked(String year, String month, String day) {
9390
public void onYearMonthDayTimePicker(View view) {
9491
DateTimePicker picker = new DateTimePicker(this, DateTimePicker.HOUR_OF_DAY);
9592
picker.setRange(2000, 2030);
96-
picker.setSelectedItem(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH),
97-
calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE));
9893
picker.setOnDateTimePickListener(new DateTimePicker.OnYearMonthDayTimePickListener() {
9994
@Override
10095
public void onDateTimePicked(String year, String month, String day, String hour, String minute) {
@@ -108,9 +103,9 @@ public void onDateTimePicked(String year, String month, String day, String hour,
108103
public void onYearMonthPicker(View view) {
109104
DatePicker picker = new DatePicker(this, DatePicker.YEAR_MONTH);
110105
picker.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
111-
picker.setRangeStart(2020, 5, 20);
106+
picker.setRangeStart(2016, 10, 14);
112107
picker.setRangeEnd(2020, 11, 11);
113-
picker.setSelectedItem(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH) + 1);
108+
picker.setSelectedItem(2016, 9);
114109
picker.setOnDatePickListener(new DatePicker.OnYearMonthPickListener() {
115110
@Override
116111
public void onDatePicked(String year, String month) {
@@ -123,7 +118,7 @@ public void onDatePicked(String year, String month) {
123118
public void onMonthDayPicker(View view) {
124119
DatePicker picker = new DatePicker(this, DatePicker.MONTH_DAY);
125120
picker.setGravity(Gravity.CENTER);//弹框居中
126-
picker.setSelectedItem(calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.DAY_OF_MONTH));
121+
picker.setSelectedItem(10, 14);
127122
picker.setOnDatePickListener(new DatePicker.OnMonthDayPickListener() {
128123
@Override
129124
public void onDatePicked(String month, String day) {
@@ -134,9 +129,9 @@ public void onDatePicked(String month, String day) {
134129
}
135130

136131
public void onTimePicker(View view) {
137-
TimePicker picker = new TimePicker(this, TimePicker.HOUR_12);
132+
TimePicker picker = new TimePicker(this, TimePicker.HOUR_24);
138133
picker.setRangeStart(9, 0);//09:00
139-
picker.setRangeEnd(12, 30);//12:30
134+
picker.setRangeEnd(18, 0);//18:30
140135
picker.setTopLineVisible(false);
141136
picker.setOnTimePickListener(new TimePicker.OnTimePickListener() {
142137
@Override

library/WheelPicker/src/main/java/cn/qqtheme/framework/picker/DatePicker.java

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Locale;
1919

2020
import cn.qqtheme.framework.util.DateUtils;
21+
import cn.qqtheme.framework.util.LogUtils;
2122
import cn.qqtheme.framework.widget.WheelView;
2223

2324
/**
@@ -27,18 +28,9 @@
2728
* @since 2015/12/14
2829
*/
2930
public class DatePicker extends WheelPicker {
30-
/**
31-
* 年月日
32-
*/
33-
public static final int YEAR_MONTH_DAY = 0;
34-
/**
35-
* 年月
36-
*/
37-
public static final int YEAR_MONTH = 1;
38-
/**
39-
* 月日
40-
*/
41-
public static final int MONTH_DAY = 2;
31+
public static final int YEAR_MONTH_DAY = 0;//年月日
32+
public static final int YEAR_MONTH = 1;//年月
33+
public static final int MONTH_DAY = 2;//月日
4234
private ArrayList<String> years = new ArrayList<String>();
4335
private ArrayList<String> months = new ArrayList<String>();
4436
private ArrayList<String> days = new ArrayList<String>();
@@ -122,6 +114,8 @@ public void setRangeStart(int startYearOrMonth, int startMonthOrDay) {
122114
this.startYear = startYearOrMonth;
123115
this.startMonth = startMonthOrDay;
124116
} else {
117+
int year = Calendar.getInstance(Locale.CHINA).get(Calendar.YEAR);
118+
startYear = endYear = year;
125119
this.startMonth = startYearOrMonth;
126120
this.startDay = startMonthOrDay;
127121
}
@@ -143,24 +137,6 @@ public void setRangeEnd(int endYearOrMonth, int endMonthOrDay) {
143137
}
144138
}
145139

146-
private int findItemIndex(ArrayList<String> items, int item) {
147-
//折半查找有序元素的索引
148-
int index = Collections.binarySearch(items, item, new Comparator<Object>() {
149-
@Override
150-
public int compare(Object lhs, Object rhs) {
151-
String lhsStr = lhs.toString();
152-
String rhsStr = rhs.toString();
153-
lhsStr = lhsStr.startsWith("0") ? lhsStr.substring(1) : lhsStr;
154-
rhsStr = rhsStr.startsWith("0") ? rhsStr.substring(1) : rhsStr;
155-
return Integer.parseInt(lhsStr) - Integer.parseInt(rhsStr);
156-
}
157-
});
158-
if (index < 0) {
159-
index = 0;
160-
}
161-
return index;
162-
}
163-
164140
/**
165141
* 设置默认选中的年月日
166142
*/
@@ -177,15 +153,17 @@ public void setSelectedItem(int year, int month, int day) {
177153
* 设置默认选中的年月或者月日
178154
*/
179155
public void setSelectedItem(int yearOrMonth, int monthOrDay) {
180-
int year = Calendar.getInstance(Locale.CHINA).get(Calendar.YEAR);
181-
changeYearData();
182156
if (mode == MONTH_DAY) {
157+
int year = Calendar.getInstance(Locale.CHINA).get(Calendar.YEAR);
158+
startYear = endYear = year;
159+
changeYearData();
183160
changeMonthData(year);
184161
changeDayData(year, yearOrMonth);
185162
selectedMonthIndex = findItemIndex(months, yearOrMonth);
186163
selectedDayIndex = findItemIndex(days, monthOrDay);
187164
} else {
188-
changeMonthData(year);
165+
changeYearData();
166+
changeMonthData(yearOrMonth);
189167
selectedYearIndex = findItemIndex(years, yearOrMonth);
190168
selectedMonthIndex = findItemIndex(months, monthOrDay);
191169
}
@@ -268,7 +246,6 @@ protected View makeCenterView() {
268246
if (!TextUtils.isEmpty(yearLabel)) {
269247
yearTextView.setText(yearLabel);
270248
}
271-
changeYearData();
272249
if (selectedYearIndex == 0) {
273250
yearView.setItems(years);
274251
} else {
@@ -319,6 +296,24 @@ public void onSelected(boolean isUserScroll, int selectedIndex, String item) {
319296
return layout;
320297
}
321298

299+
private int findItemIndex(ArrayList<String> items, int item) {
300+
//折半查找有序元素的索引,效率应该高于items.indexOf(...)
301+
int index = Collections.binarySearch(items, item, new Comparator<Object>() {
302+
@Override
303+
public int compare(Object lhs, Object rhs) {
304+
String lhsStr = lhs.toString();
305+
String rhsStr = rhs.toString();
306+
lhsStr = lhsStr.startsWith("0") ? lhsStr.substring(1) : lhsStr;
307+
rhsStr = rhsStr.startsWith("0") ? rhsStr.substring(1) : rhsStr;
308+
return Integer.parseInt(lhsStr) - Integer.parseInt(rhsStr);
309+
}
310+
});
311+
if (index < 0) {
312+
index = 0;
313+
}
314+
return index;
315+
}
316+
322317
private void changeYearData() {
323318
years.clear();
324319
if (startYear == endYear) {
@@ -356,7 +351,9 @@ private int changeMonthData(int year) {
356351
months.add(DateUtils.fillZero(i));
357352
}
358353
}
359-
selectedMonthIndex = (preSelectMonth == null || !months.contains(preSelectMonth)) ? 0 : months.indexOf(preSelectMonth);
354+
//当前设置的月份不在指定范围,则默认选中范围开始的月份
355+
int preSelectMonthIndex = preSelectMonth == null ? 0 : months.indexOf(preSelectMonth);
356+
selectedMonthIndex = preSelectMonthIndex == -1 ? 0 : preSelectMonthIndex;
360357
return DateUtils.trimZero(months.get(selectedMonthIndex));
361358
}
362359

@@ -368,12 +365,16 @@ private void changeDayData(int year, int month) {
368365
for (int i = startDay; i <= maxDays; i++) {
369366
days.add(DateUtils.fillZero(i));
370367
}
371-
selectedDayIndex = (preSelectDay == null || !days.contains(preSelectDay)) ? 0 : days.indexOf(preSelectDay);
368+
//当前设置的日子不在指定范围,则默认选中范围开始的日子
369+
int preSelectDayIndex = preSelectDay == null ? 0 : days.indexOf(preSelectDay);
370+
selectedDayIndex = preSelectDayIndex == -1 ? 0 : preSelectDayIndex;
372371
} else if (year == endYear && month == endMonth) {
373372
for (int i = 1; i <= endDay; i++) {
374373
days.add(DateUtils.fillZero(i));
375374
}
376-
selectedDayIndex = (preSelectDay == null || !days.contains(preSelectDay)) ? 0 : days.indexOf(preSelectDay);
375+
//当前设置的日子不在指定范围,则默认选中范围开始的日子
376+
int preSelectDayIndex = preSelectDay == null ? 0 : days.indexOf(preSelectDay);
377+
selectedDayIndex = preSelectDayIndex == -1 ? 0 : preSelectDayIndex;
377378
} else {
378379
for (int i = 1; i <= maxDays; i++) {
379380
days.add(DateUtils.fillZero(i));

library/WheelPicker/src/main/java/cn/qqtheme/framework/picker/TimePicker.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -229,35 +229,16 @@ public void onSubmit() {
229229
}
230230
}
231231

232-
/**
233-
* Gets selected hour.
234-
*
235-
* @return the selected hour
236-
*/
237232
public String getSelectedHour() {
238233
return selectedHour;
239234
}
240235

241-
/**
242-
* Gets selected minute.
243-
*
244-
* @return the selected minute
245-
*/
246236
public String getSelectedMinute() {
247237
return selectedMinute;
248238
}
249239

250-
/**
251-
* The interface On time pick listener.
252-
*/
253240
public interface OnTimePickListener {
254241

255-
/**
256-
* On time picked.
257-
*
258-
* @param hour the hour
259-
* @param minute the minute
260-
*/
261242
void onTimePicked(String hour, String minute);
262243

263244
}

screenshots/monthday.jpg

21.7 KB
Loading

screenshots/yearmonth.jpg

-21.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)