时间选择器
如何选择当前时间之后的时间,以当前时间10分钟后为例。
private String[] valuesHour = {"00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11","12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23"};
private String[] valuesMinute = {"00","01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11","12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24","25", "26", "27", "28", "29", "30","31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48","49", "50", "51","52", "53", "54", "55", "56", "57", "58", "59"};
private String[] valuesDays = {"今天", "明天"};
要点:
- 使用原生控件NumberPicker
numDayTv = (NumberPicker) view.findViewById(R.id.num_day);
numHourTv = (NumberPicker) view.findViewById(R.id.num_hour);
numMinute = (NumberPicker) view.findViewById(R.id.num_minute);
2.获取10分钟后的日期
Calendar calendar = Calendar.getInstance();
//将日历的分钟加10,得到10分钟后的日历
calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + 10);
//10分钟后的小时
currentHourStr = calendar.get(Calendar.HOUR_OF_DAY)+ "";
//10分钟后的分钟
currentMinuteStr = calendar.get(Calendar.MINUTE) + "";
Date date = new Date(calendar.getTimeInMillis());
SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
//得到10分钟后的日期格式
dateStr = format.format(date);
//日期day 显示
Date date1 = new Date(System.currentTimeMillis());
//今天的日期格式
todayDateStr = format.format(date1);
3.对10分钟后是今天还是明天的相关判断
numDayTv.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
if (!dateStr.equals(todayDateStr)) { //如果不是今天
List<String> stringDay = Arrays.asList(valuesDays);
ArrayList newDayList = new ArrayList();
for (int i = 0; i < valuesDays.length; i++) {
if (0 != i) { //获得数组中今天之后的list
newDayList.add(stringDay.get(i));
}
}
newDay = (String[]) newDayList.toArray(new String[newDayList.size()]); //list转化为array
numDayTv.setMaxValue(newDay.length - 1);
numDayTv.setMinValue(0);
numDayTv.setDisplayedValues(newDay);
numDayTv.setValue(0);//设置默认值
dayStr = newDay[0];
} else { //是今天
numDayTv.setMaxValue(valuesDays.length - 1);
numDayTv.setMinValue(0);
numDayTv.setDisplayedValues(valuesDays);
numDayTv.setValue(0);
dayStr = valuesDays[0];
}
4.根据今天或明天对小时的显示判断
numHourTv.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
if ("今天".equals(valuesDays[numDayTv.getValue()])) {//是今天
//array转化为list
List<String> stringHour = Arrays.asList(valuesHour);
//选择>=当前小时的list
ArrayList newHourList = new ArrayList();
for (int i = 0; i < valuesHour.length; i++) {
if (Integer.parseInt(stringHour.get(i)) >= Integer.parseInt(currentHourStr)){
newHourList.add(stringHour.get(i));
}
}
newHour = (String[]) newHourList.toArray(new String[newHourList.size()]);
numHourTv.setMaxValue(newHour.length - 1);
numHourTv.setMinValue(0);
numHourTv.setDisplayedValues(newHour);
numHourTv.setValue(0);
currentHourStr = newHour[0];
} else { //不是今天,0-23
numHourTv.setMaxValue(valuesHour.length - 1);
numHourTv.setMinValue(0);
numHourTv.setDisplayedValues(valuesHour);
numHourTv.setValue(0);
currentHourStr = valuesHour[0];
}
5.根据当前的小时获得分钟数组
numMinute.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
if (currentHourStr.equals(newHour[numHourTv.getValue()])) {
List<String> stringMin = Arrays.asList(valuesMinute);
ArrayList newMinList = new ArrayList();
for (int i = 0; i < valuesMinute.length; i++) {
if (Integer.parseInt(stringMin.get(i)) >= Integer.parseInt(currentMinuteStr)) {
newMinList.add(stringMin.get(i));
}
}
newMin = (String[]) newMinList.toArray(new String[newMinList.size()]);
numMinute.setMaxValue(newMin.length - 1);
numMinute.setMinValue(0);
numMinute.setDisplayedValues(newMin);
numMinute.setValue(0);
currentMinuteStr = newMin[0];
} else {
numMinute.setMaxValue(valuesMinute.length - 1);
numMinute.setMinValue(0);
numMinute.setDisplayedValues(valuesMinute);
numMinute.setValue(0);
currentMinuteStr = valuesMinute[0];
}
setNumberPickerDividerColor(numDayTv);
setNumberPickerDividerColor(numHourTv);
setNumberPickerDividerColor(numMinute);
6.日期滚动监听
numDayTv.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
//i表示前一个角标值,i1表示当前角标值
Log.i("", "onValueChange: i=" + i + ",i1=" + i1);
timeSelect();//获取10分钟后时间
if (!dateStr.equals(todayDateStr)) {//获得滚动后的日期
dayStr = newDay[i1];
} else {
dayStr = valuesDays[i1];
}
if (0 == i1) { //角标为0时,获得动态的时分
List<String> strings = Arrays.asList(valuesHour);
ArrayList newHourList = new ArrayList();
for (int j = 0; j < valuesHour.length; j++) {
if (Integer.parseInt(strings.get(j)) >= Integer.parseInt(currentHourStr)) {
newHourList.add(strings.get(j));
}
}
newHour = (String[]) newHourList.toArray(new String[newHourList.size()]);
numHourTv.setMaxValue(newHour.length - 1);
numHourTv.setMinValue(0);
numHourTv.setDisplayedValues(newHour);
numHourTv.setValue(0);
List<String> stringMin = Arrays.asList(valuesMinute);
ArrayList newMinList = new ArrayList();
for (int j = 0; j < valuesMinute.length; j++) {
if (Integer.parseInt(stringMin.get(j)) >= Integer.parseInt(currentMinuteStr)) {
newMinList.add(stringMin.get(j));
}
}
newMin = (String[]) newMinList.toArray(new String[newMinList.size()]);
numMinute.setMaxValue(newMin.length - 1);
numMinute.setMinValue(0);
numMinute.setDisplayedValues(newMin);
numMinute.setValue(0);
} else { //角标不为0,时为0-23,分为0-59
numMinute.setDisplayedValues(valuesMinute);
numMinute.setMaxValue(valuesMinute.length - 1);
numMinute.setMinValue(0);
numMinute.setValue(0);
numHourTv.setDisplayedValues(valuesHour);
numHourTv.setMaxValue(valuesHour.length - 1);
numHourTv.setMinValue(0);
numHourTv.setValue(0);
}
if (0 == i1) { //获得角标为0的时分
currentMinuteStr = newMin[0];
currentHourStr = newHour[0];
} else {
currentMinuteStr = valuesMinute[0];
currentHourStr = valuesHour[0];
}
}
});
7.小时滚动监听
numHourTv.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
timeSelect(); //获取10分钟后时间
//如果日期和小时角标都为0时,获得动态的分钟数组
if (0 == i1 && 0 == numDayTv.getValue()) {
List<String> stringMin = Arrays.asList(valuesMinute);
ArrayList newMinList = new ArrayList();
for (int j = 0; j < valuesMinute.length; j++) {
if (Integer.parseInt(stringMin.get(j)) >= Integer.parseInt(currentMinuteStr)) {
newMinList.add(stringMin.get(j));
}
}
newMin = (String[]) newMinList.toArray(new String[newMinList.size()]);
numMinute.setMaxValue(newMin.length - 1);
numMinute.setMinValue(0);
numMinute.setDisplayedValues(newMin);
numMinute.setValue(0);
currentMinuteStr = newMin[0];
currentHourStr = newHour[i1];
} else { //分钟数组为0-59
numMinute.setDisplayedValues(valuesMinute);
numMinute.setMaxValue(valuesMinute.length - 1);
numMinute.setMinValue(0);
numMinute.setValue(0);
currentMinuteStr = valuesMinute[0];
if (0 == numDayTv.getValue()) {
currentHourStr = newHour[i1];
} else {
currentHourStr = valuesHour[i1];
}
}
}
});
8.分钟滚动监听
numMinute.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
@Override
public void onValueChange(NumberPicker numberPicker, int i, int i1) {
if (0 == numDayTv.getValue() && 0 == numHourTv.getValue()) {
//日期和小时角标为0时的分钟
currentMinuteStr = newMin[i1];
} else {
currentMinuteStr = valuesMinute[i1];
}
}
});
//NumberPicker颜色设置
private void setNumberPickerDividerColor(NumberPicker numberPicker) {
NumberPicker picker = numberPicker;
Field[] pickerFields = NumberPicker.class.getDeclaredFields();
for (Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDivider")) {
pf.setAccessible(true);
try {
//设置分割线的颜色值
pf.set(picker, new ColorDrawable(this.getResources().getColor(R.color.checkboxnormal)));
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (Resources.NotFoundException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
break;
}
}
// 分割线高度
for (Field pf : pickerFields) {
if (pf.getName().equals("mSelectionDividerHeight")) {
pf.setAccessible(true);
try {
int result = 1;
pf.set(picker, result);
} catch (Exception e) {
e.printStackTrace();
}
break;
}
}
}