diff --git a/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java b/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java index a75304bb..99696621 100644 --- a/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java +++ b/app/src/main/java/com/bigkoo/pickerviewdemo/MainActivity.java @@ -1,19 +1,15 @@ package com.bigkoo.pickerviewdemo; -import android.app.Dialog; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; -import android.view.Gravity; import android.view.View; import android.view.ViewGroup; -import android.view.Window; import android.widget.Button; import android.widget.CheckBox; import android.widget.CompoundButton; -import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; @@ -21,13 +17,13 @@ import com.bigkoo.pickerview.builder.OptionsPickerBuilder; import com.bigkoo.pickerview.builder.TimePickerBuilder; +import com.bigkoo.pickerview.listener.CustomListener; import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener; import com.bigkoo.pickerview.listener.OnOptionsSelectListener; import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener; +import com.bigkoo.pickerview.listener.OnTimeSelectListener; import com.bigkoo.pickerview.view.OptionsPickerView; import com.bigkoo.pickerview.view.TimePickerView; -import com.bigkoo.pickerview.listener.CustomListener; -import com.bigkoo.pickerview.listener.OnTimeSelectListener; import com.bigkoo.pickerviewdemo.bean.CardBean; import com.bigkoo.pickerviewdemo.bean.ProvinceBean; @@ -204,11 +200,19 @@ public void onTimeSelectChanged(Date date) { Log.i("pvTime", "onTimeSelectChanged"); } }) - .setType(new boolean[]{true, true, true, true, true, true}) - .isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。 + // .setRangDate(Calendar.getInstance(), null) + .setType(new boolean[]{true, true, true, true, true, false}) + .setDate(Calendar.getInstance()) + .setStepHours(2) // 时钟间隔 + .setStepMinutes(5) // 分钟间隔 + .setStepSeconds(15) // 秒钟间隔 + .isCyclic(true) + .isCenterLabel(true) + // .isDialog(true) //默认设置false ,内部实现将DecorView 作为它的父控件。 + .setDecorView((ViewGroup) getWindow().getDecorView().findViewById(android.R.id.content)) .build(); - Dialog mDialog = pvTime.getDialog(); + /*Dialog mDialog = pvTime.getDialog(); if (mDialog != null) { FrameLayout.LayoutParams params = new FrameLayout.LayoutParams( @@ -225,7 +229,7 @@ public void onTimeSelectChanged(Date date) { dialogWindow.setWindowAnimations(com.bigkoo.pickerview.R.style.picker_view_slide_anim);//修改动画样式 dialogWindow.setGravity(Gravity.BOTTOM);//改成Bottom,底部显示 } - } + }*/ } diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java b/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java index a31bda81..52bb2afc 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/builder/TimePickerBuilder.java @@ -147,6 +147,35 @@ public TimePickerBuilder setRangDate(Calendar startDate, Calendar endDate) { return this; } + /** + * 设置时钟间隔 + * + * @param hoursStep + */ + public TimePickerBuilder setStepHours(int hoursStep) { + mPickerOptions.hoursStep = hoursStep; + return this; + } + + /** + * 设置分钟间隔 + * + * @param minutesStep + */ + public TimePickerBuilder setStepMinutes(int minutesStep) { + mPickerOptions.minutesStep = minutesStep; + return this; + } + + /** + * 设置秒钟间隔 + * + * @param secondsStep + */ + public TimePickerBuilder setStepSeconds(int secondsStep) { + mPickerOptions.secondsStep = secondsStep; + return this; + } /** * 设置间距倍数,但是只能在1.0-4.0f之间 diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java b/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java index 74965b9b..0b9cda84 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/configure/PickerOptions.java @@ -61,6 +61,9 @@ public class PickerOptions { public Calendar endDate;//终止时间 public int startYear;//开始年份 public int endYear;//结尾年份 + public int hoursStep;//时钟间隔 + public int minutesStep;//分钟间隔 + public int secondsStep;//秒钟间隔 public boolean cyclic = false;//是否循环 public boolean isLunarCalendar = false;//是否显示农历 diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java index 15c9d098..50a85894 100644 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/TimePickerView.java @@ -128,6 +128,7 @@ public void onTimeSelectChanged() { setRangDate(); } + setSteps(); setTime(); wheelTime.setLabels(mPickerOptions.label_year, mPickerOptions.label_month, mPickerOptions.label_day , mPickerOptions.label_hours, mPickerOptions.label_minutes, mPickerOptions.label_seconds); @@ -186,6 +187,13 @@ private void initDefaultSelectedDate() { } } + /** + * 设置时分秒的间隔, 要在setTime之前调用才有效果 + */ + private void setSteps() { + wheelTime.setSteps(mPickerOptions.hoursStep, mPickerOptions.minutesStep, mPickerOptions.secondsStep); + } + /** * 设置选中时间,默认选中当前时间 */ diff --git a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java index 5941f934..efcec17d 100755 --- a/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java +++ b/pickerview/src/main/java/com/bigkoo/pickerview/view/WheelTime.java @@ -13,6 +13,7 @@ import java.text.DateFormat; import java.text.SimpleDateFormat; +import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.List; @@ -36,6 +37,7 @@ public class WheelTime { private static final int DEFAULT_END_MONTH = 12; private static final int DEFAULT_START_DAY = 1; private static final int DEFAULT_END_DAY = 31; + private static final int DEFAULT_STEP_SIZE = 1; private int startYear = DEFAULT_START_YEAR; private int endYear = DEFAULT_END_YEAR; @@ -44,6 +46,10 @@ public class WheelTime { private int startDay = DEFAULT_START_DAY; private int endDay = DEFAULT_END_DAY; //表示31天的 private int currentYear; + + private int hoursStep = DEFAULT_STEP_SIZE; + private int minutesStep = DEFAULT_STEP_SIZE; + private int secondsStep = DEFAULT_STEP_SIZE; private int textSize; @@ -133,21 +139,24 @@ private void setLunar(int year, final int month, int day, boolean isLeap, int h, wv_day.setGravity(gravity); wv_hours = (WheelView) view.findViewById(R.id.hour); - wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); + // wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); + wv_hours.setAdapter(new ArrayWheelAdapter(generateHoursValues())); //wv_hours.setLabel(context.getString(R.string.pickerview_hours));// 添加文字 - wv_hours.setCurrentItem(h); + wv_hours.setCurrentItem(h / hoursStep); wv_hours.setGravity(gravity); wv_minutes = (WheelView) view.findViewById(R.id.min); - wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); + // wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); + wv_minutes.setAdapter(new ArrayWheelAdapter(generateMinutesValues())); //wv_minutes.setLabel(context.getString(R.string.pickerview_minutes));// 添加文字 - wv_minutes.setCurrentItem(m); + wv_minutes.setCurrentItem(m / minutesStep); wv_minutes.setGravity(gravity); wv_seconds = (WheelView) view.findViewById(R.id.second); - wv_seconds.setAdapter(new NumericWheelAdapter(0, 59)); + // wv_seconds.setAdapter(new NumericWheelAdapter(0, 59)); + wv_seconds.setAdapter(new ArrayWheelAdapter(generateSecondsValues())); //wv_seconds.setLabel(context.getString(R.string.pickerview_minutes));// 添加文字 - wv_seconds.setCurrentItem(m); + wv_seconds.setCurrentItem(s / secondsStep); wv_seconds.setGravity(gravity); // 添加"年"监听 @@ -377,21 +386,21 @@ private void setSolar(int year, final int month, int day, int h, int m, int s) { wv_day.setGravity(gravity); //时 wv_hours = (WheelView) view.findViewById(R.id.hour); - wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); - - wv_hours.setCurrentItem(h); + // wv_hours.setAdapter(new NumericWheelAdapter(0, 23)); + wv_hours.setAdapter(new ArrayWheelAdapter(generateHoursValues())); + wv_hours.setCurrentItem(h / hoursStep); wv_hours.setGravity(gravity); //分 wv_minutes = (WheelView) view.findViewById(R.id.min); - wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); - - wv_minutes.setCurrentItem(m); + // wv_minutes.setAdapter(new NumericWheelAdapter(0, 59)); + wv_minutes.setAdapter(new ArrayWheelAdapter(generateMinutesValues())); + wv_minutes.setCurrentItem(m / minutesStep); wv_minutes.setGravity(gravity); //秒 wv_seconds = (WheelView) view.findViewById(R.id.second); - wv_seconds.setAdapter(new NumericWheelAdapter(0, 59)); - - wv_seconds.setCurrentItem(s); + // wv_seconds.setAdapter(new NumericWheelAdapter(0, 59)); + wv_seconds.setAdapter(new ArrayWheelAdapter(generateSecondsValues())); + wv_seconds.setCurrentItem(s / secondsStep); wv_seconds.setGravity(gravity); // 添加"年"监听 @@ -723,25 +732,25 @@ public String getTime() { sb.append((wv_year.getCurrentItem() + startYear)).append("-") .append((wv_month.getCurrentItem() + startMonth)).append("-") .append((wv_day.getCurrentItem() + startDay)).append(" ") - .append(wv_hours.getCurrentItem()).append(":") - .append(wv_minutes.getCurrentItem()).append(":") - .append(wv_seconds.getCurrentItem()); + .append(wv_hours.getCurrentItem() * hoursStep).append(":") + .append(wv_minutes.getCurrentItem() * minutesStep).append(":") + .append(wv_seconds.getCurrentItem() * secondsStep); } else { sb.append((wv_year.getCurrentItem() + startYear)).append("-") .append((wv_month.getCurrentItem() + startMonth)).append("-") .append((wv_day.getCurrentItem() + 1)).append(" ") - .append(wv_hours.getCurrentItem()).append(":") - .append(wv_minutes.getCurrentItem()).append(":") - .append(wv_seconds.getCurrentItem()); + .append(wv_hours.getCurrentItem() * hoursStep).append(":") + .append(wv_minutes.getCurrentItem() * minutesStep).append(":") + .append(wv_seconds.getCurrentItem() * secondsStep); } } else { sb.append((wv_year.getCurrentItem() + startYear)).append("-") .append((wv_month.getCurrentItem() + 1)).append("-") .append((wv_day.getCurrentItem() + 1)).append(" ") - .append(wv_hours.getCurrentItem()).append(":") - .append(wv_minutes.getCurrentItem()).append(":") - .append(wv_seconds.getCurrentItem()); + .append(wv_hours.getCurrentItem() * hoursStep).append(":") + .append(wv_minutes.getCurrentItem() * minutesStep).append(":") + .append(wv_seconds.getCurrentItem() * secondsStep); } return sb.toString(); @@ -776,12 +785,36 @@ private String getLunarTime() { sb.append(solar[0]).append("-") .append(solar[1]).append("-") .append(solar[2]).append(" ") - .append(wv_hours.getCurrentItem()).append(":") - .append(wv_minutes.getCurrentItem()).append(":") - .append(wv_seconds.getCurrentItem()); + .append(wv_hours.getCurrentItem() * hoursStep).append(":") + .append(wv_minutes.getCurrentItem() * minutesStep).append(":") + .append(wv_seconds.getCurrentItem() * secondsStep); return sb.toString(); } + private List generateHoursValues() { + final List hours = new ArrayList<>(); + for (int h = 0; h <= 23; h += hoursStep) { + hours.add(h); + } + return hours; + } + + private List generateMinutesValues() { + final List minutes = new ArrayList<>(); + for (int m = 0; m <= 59; m += minutesStep) { + minutes.add(m); + } + return minutes; + } + + private List generateSecondsValues() { + final List seconds = new ArrayList<>(); + for (int s = 0; s <= 59; s += secondsStep) { + seconds.add(s); + } + return seconds; + } + public View getView() { return view; } @@ -864,6 +897,18 @@ public void setRangDate(Calendar startDate, Calendar endDate) { } + public void setSteps(int hoursStep, int minutesStep, int secondsStep) { + if (hoursStep < 24 && hoursStep > 0) { + this.hoursStep = hoursStep; + } + if (minutesStep < 60 && minutesStep > 0) { + this.minutesStep = minutesStep; + } + if (secondsStep < 60 && secondsStep > 0) { + this.secondsStep = secondsStep; + } + } + /** * 设置间距倍数,但是只能在1.0-4.0f之间 *