基于PickerView改造的开始结束时间选择器,PickerView是一款很好用的选择器控件,它的时间选择器TimePickerView和条件选择器OptionsPickerView我至今都还在用,可惜已经不维护了!首先我们要引入它:
implementation 'com.contrarywind:Android-PickerView:4.1.9'
-
需求:尽管TimePickerView很好用,但也许你会遇到用户要求一次把开始时间和结束时间都同时选择的情况,而不是点击一个地方让用户选择开始时间,再点击一个地方让用户选择结束时间!于是不得不改造一个弹框出来,同时选择开始和结束时间,大概如下所示!
image.png
1.重写三个文件
文件一TimePickerBuilder2
import android.content.Context
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorInt
import com.bigkoo.pickerview.configure.PickerOptions
import com.bigkoo.pickerview.listener.CustomListener
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener
import com.contrarywind.view.WheelView.DividerType
import java.util.*
class TimePickerBuilder2(context: Context?, private val listener: (dateStart:Date, dateEnd:Date)->Unit) {
private val mPickerOptions: PickerOptions = PickerOptions(PickerOptions.TYPE_PICKER_TIME)
//Option
fun setGravity(gravity: Int): TimePickerBuilder2 {
mPickerOptions.textGravity = gravity
return this
}
fun addOnCancelClickListener(cancelListener: View.OnClickListener?): TimePickerBuilder2 {
mPickerOptions.cancelListener = cancelListener
return this
}
fun setType(type: BooleanArray?): TimePickerBuilder2 {
mPickerOptions.type = type
return this
}
fun setSubmitText(textContentConfirm: String?): TimePickerBuilder2 {
mPickerOptions.textContentConfirm = textContentConfirm
return this
}
fun isDialog(isDialog: Boolean): TimePickerBuilder2 {
mPickerOptions.isDialog = isDialog
return this
}
fun setCancelText(textContentCancel: String?): TimePickerBuilder2 {
mPickerOptions.textContentCancel = textContentCancel
return this
}
fun setTitleText(textContentTitle: String?): TimePickerBuilder2 {
mPickerOptions.textContentTitle = textContentTitle
return this
}
fun setSubmitColor(textColorConfirm: Int): TimePickerBuilder2 {
mPickerOptions.textColorConfirm = textColorConfirm
return this
}
fun setCancelColor(textColorCancel: Int): TimePickerBuilder2 {
mPickerOptions.textColorCancel = textColorCancel
return this
}
/**
* ViewGroup 类型的容器
*
* @param decorView 选择器会被添加到此容器中
* @return TimePickerBuilder
*/
fun setDecorView(decorView: ViewGroup?): TimePickerBuilder2 {
mPickerOptions.decorView = decorView
return this
}
fun setBgColor(bgColorWheel: Int): TimePickerBuilder2 {
mPickerOptions.bgColorWheel = bgColorWheel
return this
}
fun setTitleBgColor(bgColorTitle: Int): TimePickerBuilder2 {
mPickerOptions.bgColorTitle = bgColorTitle
return this
}
fun setTitleColor(textColorTitle: Int): TimePickerBuilder2 {
mPickerOptions.textColorTitle = textColorTitle
return this
}
fun setSubCalSize(textSizeSubmitCancel: Int): TimePickerBuilder2 {
mPickerOptions.textSizeSubmitCancel = textSizeSubmitCancel
return this
}
fun setTitleSize(textSizeTitle: Int): TimePickerBuilder2 {
mPickerOptions.textSizeTitle = textSizeTitle
return this
}
fun setContentTextSize(textSizeContent: Int): TimePickerBuilder2 {
mPickerOptions.textSizeContent = textSizeContent
return this
}
/**
* 设置最大可见数目
*
* @param count suggest value: 3, 5, 7, 9
*/
fun setItemVisibleCount(count: Int): TimePickerBuilder2 {
mPickerOptions.itemsVisibleCount = count
return this
}
/**
* 透明度是否渐变
*
* @param isAlphaGradient true of false
*/
fun isAlphaGradient(isAlphaGradient: Boolean): TimePickerBuilder2 {
mPickerOptions.isAlphaGradient = isAlphaGradient
return this
}
/**
* 因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
*
* @param date
* @return TimePickerBuilder
*/
fun setDate(date: Calendar?): TimePickerBuilder2 {
mPickerOptions.date = date
return this
}
fun setLayoutRes(res: Int, customListener: CustomListener?): TimePickerBuilder2 {
mPickerOptions.layoutRes = res
mPickerOptions.customListener = customListener
return this
}
/**
* 设置起始时间
* 因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
*/
fun setRangDate(startDate: Calendar?, endDate: Calendar?): TimePickerBuilder2 {
mPickerOptions.startDate = startDate
mPickerOptions.endDate = endDate
return this
}
/**
* 设置间距倍数,但是只能在1.0-4.0f之间
*
* @param lineSpacingMultiplier
*/
fun setLineSpacingMultiplier(lineSpacingMultiplier: Float): TimePickerBuilder2 {
mPickerOptions.lineSpacingMultiplier = lineSpacingMultiplier
return this
}
/**
* 设置分割线的颜色
*
* @param dividerColor
*/
fun setDividerColor(@ColorInt dividerColor: Int): TimePickerBuilder2 {
mPickerOptions.dividerColor = dividerColor
return this
}
/**
* 设置分割线的类型
*
* @param dividerType
*/
fun setDividerType(dividerType: DividerType?): TimePickerBuilder2 {
mPickerOptions.dividerType = dividerType
return this
}
/**
* [.setOutSideColor] instead.
*
* @param backgroundId color resId.
*/
@Deprecated("")
fun setBackgroundId(backgroundId: Int): TimePickerBuilder2 {
mPickerOptions.outSideColor = backgroundId
return this
}
/**
* 显示时的外部背景色颜色,默认是灰色
*
* @param outSideColor
*/
fun setOutSideColor(@ColorInt outSideColor: Int): TimePickerBuilder2 {
mPickerOptions.outSideColor = outSideColor
return this
}
/**
* 设置分割线之间的文字的颜色
*
* @param textColorCenter
*/
fun setTextColorCenter(@ColorInt textColorCenter: Int): TimePickerBuilder2 {
mPickerOptions.textColorCenter = textColorCenter
return this
}
/**
* 设置分割线以外文字的颜色
*
* @param textColorOut
*/
fun setTextColorOut(@ColorInt textColorOut: Int): TimePickerBuilder2 {
mPickerOptions.textColorOut = textColorOut
return this
}
fun isCyclic(cyclic: Boolean): TimePickerBuilder2 {
mPickerOptions.cyclic = cyclic
return this
}
fun setOutSideCancelable(cancelable: Boolean): TimePickerBuilder2 {
mPickerOptions.cancelable = cancelable
return this
}
fun setLabel(
label_year: String?,
label_month: String?,
label_day: String?
): TimePickerBuilder2 {
mPickerOptions.label_year = label_year
mPickerOptions.label_month = label_month
mPickerOptions.label_day = label_day
return this
}
/**
* 设置X轴倾斜角度[ -90 , 90°]
*
* @param x_offset_year 年
* @param x_offset_month 月
* @param x_offset_day 日
* @return
*/
fun setTextXOffset(
x_offset_year: Int,
x_offset_month: Int,
x_offset_day: Int
): TimePickerBuilder2 {
mPickerOptions.x_offset_year = x_offset_year
mPickerOptions.x_offset_month = x_offset_month
mPickerOptions.x_offset_day = x_offset_day
return this
}
fun isCenterLabel(isCenterLabel: Boolean): TimePickerBuilder2 {
mPickerOptions.isCenterLabel = isCenterLabel
return this
}
fun setTimeSelectChangeListener(listener: OnTimeSelectChangeListener?): TimePickerBuilder2 {
mPickerOptions.timeSelectChangeListener = listener
return this
}
fun build(): TimePickerView2 {
return TimePickerView2(mPickerOptions, listener)
}
init {
mPickerOptions.context = context
}
}
文件二TimePickerView2
import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.bigkoo.pickerview.configure.PickerOptions
import com.bigkoo.pickerview.view.BasePickerView
import java.text.ParseException
import java.util.*
class TimePickerView2(pickerOptions: PickerOptions,val listener:(dateStart:Date,dateEnd:Date)->Unit) : BasePickerView(pickerOptions.context),
View.OnClickListener {
private var wheelTime2 //自定义控件
: WheelTime2? = null
private fun initView(context: Context) {
setDialogOutSideCancelable()
initViews()
initAnim()
if (mPickerOptions.customListener == null) {
LayoutInflater.from(context).inflate(R.layout.pickerview_time2, contentContainer)
//顶部标题
val tvTitle = findViewById(R.id.tvTitle) as TextView
val rvTopBar = findViewById(R.id.rv_topbar) as RelativeLayout
//确定和取消按钮
val btnSubmit = findViewById(R.id.btnSubmit) as Button
val btnCancel = findViewById(R.id.btnCancel) as Button
btnSubmit.tag = TAG_SUBMIT
btnCancel.tag = TAG_CANCEL
btnSubmit.setOnClickListener(this)
btnCancel.setOnClickListener(this)
//设置文字
btnSubmit.text =
if (TextUtils.isEmpty(mPickerOptions.textContentConfirm)) "确定" else mPickerOptions.textContentConfirm
btnCancel.text =
if (TextUtils.isEmpty(mPickerOptions.textContentCancel)) "取消" else mPickerOptions.textContentCancel
tvTitle.text =
if (TextUtils.isEmpty(mPickerOptions.textContentTitle)) "" else mPickerOptions.textContentTitle //默认为空
//设置color
btnSubmit.setTextColor(mPickerOptions.textColorConfirm)
btnCancel.setTextColor(mPickerOptions.textColorCancel)
tvTitle.setTextColor(mPickerOptions.textColorTitle)
rvTopBar.setBackgroundColor(mPickerOptions.bgColorTitle)
//设置文字大小
btnSubmit.textSize = mPickerOptions.textSizeSubmitCancel.toFloat()
btnCancel.textSize = mPickerOptions.textSizeSubmitCancel.toFloat()
tvTitle.textSize = mPickerOptions.textSizeTitle.toFloat()
} else {
mPickerOptions.customListener.customLayout(
LayoutInflater.from(context).inflate(mPickerOptions.layoutRes, contentContainer)
)
}
// 时间转轮 自定义控件
val timePickerView = findViewById(R.id.timepicker) as LinearLayout
val timePickerView2 = findViewById(R.id.timepicker2) as LinearLayout
timePickerView.setBackgroundColor(mPickerOptions.bgColorWheel)
timePickerView2.setBackgroundColor(mPickerOptions.bgColorWheel)
initWheelTime2(timePickerView, timePickerView2)
}
private fun initWheelTime2(timePickerView: LinearLayout, timePickerView2: LinearLayout) {
wheelTime2 = WheelTime2(
timePickerView,
timePickerView2,
mPickerOptions.type,
mPickerOptions.textGravity,
mPickerOptions.textSizeContent
)
// if (listener != null) {
// wheelTime2!!.setSelectChangeCallback {
// try {
// val date = WheelTime2.dateFormat.parse(wheelTime2!!.time)
// val date2 = WheelTime2.dateFormat.parse(wheelTime2!!.time2)
// listener(date,date2)
//
// } catch (e: ParseException) {
// e.printStackTrace()
// }
// }
// }
if (mPickerOptions.startYear != 0 && mPickerOptions.endYear != 0 && mPickerOptions.startYear <= mPickerOptions.endYear) {
setRange()
}
//若手动设置了时间范围限制
if (mPickerOptions.startDate != null && mPickerOptions.endDate != null) {
require(mPickerOptions.startDate.timeInMillis <= mPickerOptions.endDate.timeInMillis) { "startDate can't be later than endDate" }
setRangDate()
} else if (mPickerOptions.startDate != null) {
require(mPickerOptions.startDate[Calendar.YEAR] >= 1900) { "The startDate can not as early as 1900" }
setRangDate()
} else if (mPickerOptions.endDate != null) {
require(mPickerOptions.endDate[Calendar.YEAR] <= 2100) { "The endDate should not be later than 2100" }
setRangDate()
} else { //没有设置时间范围限制,则会使用默认范围。
setRangDate()
}
setTime()
wheelTime2!!.setLabels(
mPickerOptions.label_year,
mPickerOptions.label_month,
mPickerOptions.label_day
)
wheelTime2!!.setTextXOffset(
mPickerOptions.x_offset_year,
mPickerOptions.x_offset_month,
mPickerOptions.x_offset_day
)
wheelTime2!!.setItemsVisible(mPickerOptions.itemsVisibleCount)
wheelTime2!!.setAlphaGradient(mPickerOptions.isAlphaGradient)
setOutSideCancelable(mPickerOptions.cancelable)
wheelTime2!!.setCyclic(mPickerOptions.cyclic)
wheelTime2!!.setDividerColor(mPickerOptions.dividerColor)
wheelTime2!!.setDividerType(mPickerOptions.dividerType)
wheelTime2!!.setLineSpacingMultiplier(mPickerOptions.lineSpacingMultiplier)
wheelTime2!!.setTextColorOut(mPickerOptions.textColorOut)
wheelTime2!!.setTextColorCenter(mPickerOptions.textColorCenter)
wheelTime2!!.isCenterLabel(mPickerOptions.isCenterLabel)
}
/**
* 设置默认时间
*/
fun setDate(date: Calendar?) {
mPickerOptions.date = date
setTime()
}
/**
* 设置可以选择的时间范围, 要在setTime之前调用才有效果
*/
private fun setRange() {
wheelTime2!!.startYear = mPickerOptions.startYear
wheelTime2!!.endYear = mPickerOptions.endYear
}
/**
* 设置可以选择的时间范围, 要在setTime之前调用才有效果
*/
private fun setRangDate() {
wheelTime2!!.setRangDate(mPickerOptions.startDate, mPickerOptions.endDate)
initDefaultSelectedDate()
}
private fun initDefaultSelectedDate() {
//如果手动设置了时间范围
if (mPickerOptions.startDate != null && mPickerOptions.endDate != null) {
//若默认时间未设置,或者设置的默认时间越界了,则设置默认选中时间为开始时间。
if (mPickerOptions.date == null || mPickerOptions.date.timeInMillis < mPickerOptions.startDate.timeInMillis || mPickerOptions.date.timeInMillis > mPickerOptions.endDate.timeInMillis) {
mPickerOptions.date = mPickerOptions.startDate
}
} else if (mPickerOptions.startDate != null) {
//没有设置默认选中时间,那就拿开始时间当默认时间
mPickerOptions.date = mPickerOptions.startDate
} else if (mPickerOptions.endDate != null) {
mPickerOptions.date = mPickerOptions.endDate
}
}
/**
* 设置选中时间,默认选中当前时间
*/
private fun setTime() {
val year: Int
val month: Int
val day: Int
var hours: Int
var minute: Int
var seconds: Int
val calendar = Calendar.getInstance()
if (mPickerOptions.date == null) {
calendar.timeInMillis = System.currentTimeMillis()
year = calendar[Calendar.YEAR]
month = calendar[Calendar.MONTH]
day = calendar[Calendar.DAY_OF_MONTH]
} else {
year = mPickerOptions.date[Calendar.YEAR]
month = mPickerOptions.date[Calendar.MONTH]
day = mPickerOptions.date[Calendar.DAY_OF_MONTH]
}
wheelTime2!!.setPicker(year, month, day)
}
override fun onClick(v: View) {
val tag = v.tag as String
if (tag == TAG_SUBMIT) {
returnData()
} else if (tag == TAG_CANCEL) {
if (mPickerOptions.cancelListener != null) {
mPickerOptions.cancelListener.onClick(v)
}
}
dismiss()
}
private fun returnData() {
if (listener != null) {
try {
val date = WheelTime2.dateFormat.parse(wheelTime2!!.time)
val date2 = WheelTime2.dateFormat.parse(wheelTime2!!.time2)
listener(date,date2)
} catch (e: ParseException) {
e.printStackTrace()
}
}
}
/**
* 动态设置标题
*
* @param text 标题文本内容
*/
fun setTitleText(text: String?) {
val tvTitle = findViewById(R.id.tvTitle) as TextView
if (tvTitle != null) {
tvTitle.text = text
}
}
override fun isDialog(): Boolean {
return mPickerOptions.isDialog
}
companion object {
private const val TAG_SUBMIT = "submit"
private const val TAG_CANCEL = "cancel"
}
init {
mPickerOptions = pickerOptions
initView(pickerOptions.context)
}
}
文件三WheelTime2
import android.view.View
import com.bigkoo.pickerview.adapter.NumericWheelAdapter
import com.bigkoo.pickerview.listener.ISelectTimeCallback
import com.contrarywind.view.WheelView
import com.contrarywind.view.WheelView.DividerType
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
class WheelTime2(
private val view: View,
private val view2: View,
private val type: BooleanArray,
private val gravity: Int,
private val textSize: Int
) {
private var wv_year: WheelView? = null
private var wv_month: WheelView? = null
private var wv_day: WheelView? = null
private var wv_year2: WheelView? = null
private var wv_month2: WheelView? = null
private var wv_day2: WheelView? = null
var startYear = DEFAULT_START_YEAR
var endYear = DEFAULT_END_YEAR
private var startMonth = DEFAULT_START_MONTH
private var endMonth = DEFAULT_END_MONTH
private var startDay = DEFAULT_START_DAY
private var endDay = DEFAULT_END_DAY //表示31天的
private var currentYear = 0
private var mSelectChangeCallback: ISelectTimeCallback? = null
fun setPicker(year: Int, month: Int, day: Int) {
setSolar(year, month, day)
setSolar2(year, month, day)
setContentTextSize()
}
/**
* 设置公历
*
* @param year
* @param month
* @param day
*/
private fun setSolar(year: Int, month: Int, day: Int) {
// 添加大小月月份并将其转换为list,方便之后的判断
val months_big = arrayOf("1", "3", "5", "7", "8", "10", "12")
val months_little = arrayOf("4", "6", "9", "11")
val list_big = Arrays.asList(*months_big)
val list_little = Arrays.asList(*months_little)
currentYear = year
// 年
wv_year = view.findViewById<View>(R.id.year) as WheelView
wv_year!!.adapter = NumericWheelAdapter(startYear, endYear) // 设置"年"的显示数据
wv_year!!.currentItem = year - startYear // 初始化时显示的数据
wv_year!!.setGravity(gravity)
// 月
wv_month = view.findViewById<View>(R.id.month) as WheelView
if (startYear == endYear) { //开始年等于终止年
wv_month!!.adapter = NumericWheelAdapter(startMonth, endMonth)
wv_month!!.currentItem = month + 1 - startMonth
} else if (year == startYear) {
//起始日期的月份控制
wv_month!!.adapter = NumericWheelAdapter(startMonth, 12)
wv_month!!.currentItem = month + 1 - startMonth
} else if (year == endYear) {
//终止日期的月份控制
wv_month!!.adapter = NumericWheelAdapter(1, endMonth)
wv_month!!.currentItem = month
} else {
wv_month!!.adapter = NumericWheelAdapter(1, 12)
wv_month!!.currentItem = month
}
wv_month!!.setGravity(gravity)
// 日
wv_day = view.findViewById<View>(R.id.day) as WheelView
val leapYear = year % 4 == 0 && year % 100 != 0 || year % 400 == 0
if (startYear == endYear && startMonth == endMonth) {
if (list_big.contains((month + 1).toString())) {
if (endDay > 31) {
endDay = 31
}
wv_day!!.adapter = NumericWheelAdapter(startDay, endDay)
} else if (list_little.contains((month + 1).toString())) {
if (endDay > 30) {
endDay = 30
}
wv_day!!.adapter = NumericWheelAdapter(startDay, endDay)
} else {
// 闰年
if (leapYear) {
if (endDay > 29) {
endDay = 29
}
wv_day!!.adapter = NumericWheelAdapter(startDay, endDay)
} else {
if (endDay > 28) {
endDay = 28
}
wv_day!!.adapter = NumericWheelAdapter(startDay, endDay)
}
}
wv_day!!.currentItem = day - startDay
} else if (year == startYear && month + 1 == startMonth) {
// 起始日期的天数控制
if (list_big.contains((month + 1).toString())) {
wv_day!!.adapter = NumericWheelAdapter(startDay, 31)
} else if (list_little.contains((month + 1).toString())) {
wv_day!!.adapter = NumericWheelAdapter(startDay, 30)
} else {
// 闰年 29,平年 28
wv_day!!.adapter = NumericWheelAdapter(startDay, if (leapYear) 29 else 28)
}
wv_day!!.currentItem = day - startDay
} else if (year == endYear && month + 1 == endMonth) {
// 终止日期的天数控制
if (list_big.contains((month + 1).toString())) {
if (endDay > 31) {
endDay = 31
}
wv_day!!.adapter = NumericWheelAdapter(1, endDay)
} else if (list_little.contains((month + 1).toString())) {
if (endDay > 30) {
endDay = 30
}
wv_day!!.adapter = NumericWheelAdapter(1, endDay)
} else {
// 闰年
if (leapYear) {
if (endDay > 29) {
endDay = 29
}
wv_day!!.adapter = NumericWheelAdapter(1, endDay)
} else {
if (endDay > 28) {
endDay = 28
}
wv_day!!.adapter = NumericWheelAdapter(1, endDay)
}
}
wv_day!!.currentItem = day - 1
} else {
// 判断大小月及是否闰年,用来确定"日"的数据
if (list_big.contains((month + 1).toString())) {
wv_day!!.adapter = NumericWheelAdapter(1, 31)
} else if (list_little.contains((month + 1).toString())) {
wv_day!!.adapter = NumericWheelAdapter(1, 30)
} else {
// 闰年 29,平年 28
wv_day!!.adapter = NumericWheelAdapter(startDay, if (leapYear) 29 else 28)
}
wv_day!!.currentItem = day - 1
}
wv_day!!.setGravity(gravity)
// 添加"年"监听
wv_year!!.setOnItemSelectedListener { index ->
val year_num = index + startYear
currentYear = year_num
var currentMonthItem = wv_month!!.currentItem //记录上一次的item位置
// 判断大小月及是否闰年,用来确定"日"的数据
if (startYear == endYear) {
//重新设置月份
wv_month!!.adapter = NumericWheelAdapter(startMonth, endMonth)
if (currentMonthItem > wv_month!!.adapter.itemsCount - 1) {
currentMonthItem = wv_month!!.adapter.itemsCount - 1
wv_month!!.currentItem = currentMonthItem
}
val monthNum = currentMonthItem + startMonth
if (startMonth == endMonth) {
//重新设置日
setReDay(year_num, monthNum, startDay, endDay, list_big, list_little)
} else if (monthNum == startMonth) {
//重新设置日
setReDay(year_num, monthNum, startDay, 31, list_big, list_little)
} else if (monthNum == endMonth) {
setReDay(year_num, monthNum, 1, endDay, list_big, list_little)
} else { //重新设置日
setReDay(year_num, monthNum, 1, 31, list_big, list_little)
}
} else if (year_num == startYear) { //等于开始的年
//重新设置月份
wv_month!!.adapter = NumericWheelAdapter(startMonth, 12)
if (currentMonthItem > wv_month!!.adapter.itemsCount - 1) {
currentMonthItem = wv_month!!.adapter.itemsCount - 1
wv_month!!.currentItem = currentMonthItem
}
val month = currentMonthItem + startMonth
if (month == startMonth) {
//重新设置日
setReDay(year_num, month, startDay, 31, list_big, list_little)
} else {
//重新设置日
setReDay(year_num, month, 1, 31, list_big, list_little)
}
} else if (year_num == endYear) {
//重新设置月份
wv_month!!.adapter = NumericWheelAdapter(1, endMonth)
if (currentMonthItem > wv_month!!.adapter.itemsCount - 1) {
currentMonthItem = wv_month!!.adapter.itemsCount - 1
wv_month!!.currentItem = currentMonthItem
}
val monthNum = currentMonthItem + 1
if (monthNum == endMonth) {
//重新设置日
setReDay(year_num, monthNum, 1, endDay, list_big, list_little)
} else {
//重新设置日
setReDay(year_num, monthNum, 1, 31, list_big, list_little)
}
} else {
//重新设置月份
wv_month!!.adapter = NumericWheelAdapter(1, 12)
//重新设置日
setReDay(year_num, wv_month!!.currentItem + 1, 1, 31, list_big, list_little)
}
if (mSelectChangeCallback != null) {
mSelectChangeCallback!!.onTimeSelectChanged()
}
}
// 添加"月"监听
wv_month!!.setOnItemSelectedListener { index ->
var month_num = index + 1
if (startYear == endYear) {
month_num = month_num + startMonth - 1
if (startMonth == endMonth) {
//重新设置日
setReDay(currentYear, month_num, startDay, endDay, list_big, list_little)
} else if (startMonth == month_num) {
//重新设置日
setReDay(currentYear, month_num, startDay, 31, list_big, list_little)
} else if (endMonth == month_num) {
setReDay(currentYear, month_num, 1, endDay, list_big, list_little)
} else {
setReDay(currentYear, month_num, 1, 31, list_big, list_little)
}
} else if (currentYear == startYear) {
month_num = month_num + startMonth - 1
if (month_num == startMonth) {
//重新设置日
setReDay(currentYear, month_num, startDay, 31, list_big, list_little)
} else {
//重新设置日
setReDay(currentYear, month_num, 1, 31, list_big, list_little)
}
} else if (currentYear == endYear) {
if (month_num == endMonth) {
//重新设置日
setReDay(
currentYear,
wv_month!!.currentItem + 1,
1,
endDay,
list_big,
list_little
)
} else {
setReDay(currentYear, wv_month!!.currentItem + 1, 1, 31, list_big, list_little)
}
} else {
//重新设置日
setReDay(currentYear, month_num, 1, 31, list_big, list_little)
}
if (mSelectChangeCallback != null) {
mSelectChangeCallback!!.onTimeSelectChanged()
}
}
setChangedListener(wv_day)
require(type.size == 6) { "type[] length is not 6" }
wv_year!!.visibility = if (type[0]) View.VISIBLE else View.GONE
wv_month!!.visibility = if (type[1]) View.VISIBLE else View.GONE
wv_day!!.visibility = if (type[2]) View.VISIBLE else View.GONE
}
private fun setSolar2(year: Int, month: Int, day: Int) {
// 添加大小月月份并将其转换为list,方便之后的判断
val months_big = arrayOf("1", "3", "5", "7", "8", "10", "12")
val months_little = arrayOf("4", "6", "9", "11")
val list_big = Arrays.asList(*months_big)
val list_little = Arrays.asList(*months_little)
currentYear = year
// 年
wv_year2 = view2.findViewById<View>(R.id.year2) as WheelView
wv_year2!!.adapter = NumericWheelAdapter(startYear, endYear) // 设置"年"的显示数据
wv_year2!!.currentItem = year - startYear // 初始化时显示的数据
wv_year2!!.setGravity(gravity)
// 月
wv_month2 = view2.findViewById<View>(R.id.month2) as WheelView
if (startYear == endYear) { //开始年等于终止年
wv_month2!!.adapter = NumericWheelAdapter(startMonth, endMonth)
wv_month2!!.currentItem = month + 1 - startMonth
} else if (year == startYear) {
//起始日期的月份控制
wv_month2!!.adapter = NumericWheelAdapter(startMonth, 12)
wv_month2!!.currentItem = month + 1 - startMonth
} else if (year == endYear) {
//终止日期的月份控制
wv_month2!!.adapter = NumericWheelAdapter(1, endMonth)
wv_month2!!.currentItem = month
} else {
wv_month2!!.adapter = NumericWheelAdapter(1, 12)
wv_month2!!.currentItem = month
}
wv_month2!!.setGravity(gravity)
// 日
wv_day2 = view2.findViewById<View>(R.id.day2) as WheelView
val leapYear = year % 4 == 0 && year % 100 != 0 || year % 400 == 0
if (startYear == endYear && startMonth == endMonth) {
if (list_big.contains((month + 1).toString())) {
if (endDay > 31) {
endDay = 31
}
wv_day2!!.adapter = NumericWheelAdapter(startDay, endDay)
} else if (list_little.contains((month + 1).toString())) {
if (endDay > 30) {
endDay = 30
}
wv_day2!!.adapter = NumericWheelAdapter(startDay, endDay)
} else {
// 闰年
if (leapYear) {
if (endDay > 29) {
endDay = 29
}
wv_day2!!.adapter = NumericWheelAdapter(startDay, endDay)
} else {
if (endDay > 28) {
endDay = 28
}
wv_day2!!.adapter = NumericWheelAdapter(startDay, endDay)
}
}
wv_day2!!.currentItem = day - startDay
} else if (year == startYear && month + 1 == startMonth) {
// 起始日期的天数控制
if (list_big.contains((month + 1).toString())) {
wv_day2!!.adapter = NumericWheelAdapter(startDay, 31)
} else if (list_little.contains((month + 1).toString())) {
wv_day2!!.adapter = NumericWheelAdapter(startDay, 30)
} else {
// 闰年 29,平年 28
wv_day2!!.adapter = NumericWheelAdapter(startDay, if (leapYear) 29 else 28)
}
wv_day2!!.currentItem = day - startDay
} else if (year == endYear && month + 1 == endMonth) {
// 终止日期的天数控制
if (list_big.contains((month + 1).toString())) {
if (endDay > 31) {
endDay = 31
}
wv_day2!!.adapter = NumericWheelAdapter(1, endDay)
} else if (list_little.contains((month + 1).toString())) {
if (endDay > 30) {
endDay = 30
}
wv_day2!!.adapter = NumericWheelAdapter(1, endDay)
} else {
// 闰年
if (leapYear) {
if (endDay > 29) {
endDay = 29
}
wv_day!!.adapter = NumericWheelAdapter(1, endDay)
} else {
if (endDay > 28) {
endDay = 28
}
wv_day2!!.adapter = NumericWheelAdapter(1, endDay)
}
}
wv_day2!!.currentItem = day - 1
} else {
// 判断大小月及是否闰年,用来确定"日"的数据
if (list_big.contains((month + 1).toString())) {
wv_day2!!.adapter = NumericWheelAdapter(1, 31)
} else if (list_little.contains((month + 1).toString())) {
wv_day2!!.adapter = NumericWheelAdapter(1, 30)
} else {
// 闰年 29,平年 28
wv_day2!!.adapter = NumericWheelAdapter(startDay, if (leapYear) 29 else 28)
}
wv_day2!!.currentItem = day - 1
}
wv_day2!!.setGravity(gravity)
// 添加"年"监听
wv_year2!!.setOnItemSelectedListener { index ->
val year_num = index + startYear
currentYear = year_num
var currentMonthItem = wv_month2!!.currentItem //记录上一次的item位置
// 判断大小月及是否闰年,用来确定"日"的数据
if (startYear == endYear) {
//重新设置月份
wv_month2!!.adapter = NumericWheelAdapter(startMonth, endMonth)
if (currentMonthItem > wv_month2!!.adapter.itemsCount - 1) {
currentMonthItem = wv_month2!!.adapter.itemsCount - 1
wv_month2!!.currentItem = currentMonthItem
}
val monthNum = currentMonthItem + startMonth
if (startMonth == endMonth) {
//重新设置日
setReDay(year_num, monthNum, startDay, endDay, list_big, list_little)
} else if (monthNum == startMonth) {
//重新设置日
setReDay(year_num, monthNum, startDay, 31, list_big, list_little)
} else if (monthNum == endMonth) {
setReDay(year_num, monthNum, 1, endDay, list_big, list_little)
} else { //重新设置日
setReDay(year_num, monthNum, 1, 31, list_big, list_little)
}
} else if (year_num == startYear) { //等于开始的年
//重新设置月份
wv_month2!!.adapter = NumericWheelAdapter(startMonth, 12)
if (currentMonthItem > wv_month2!!.adapter.itemsCount - 1) {
currentMonthItem = wv_month2!!.adapter.itemsCount - 1
wv_month2!!.currentItem = currentMonthItem
}
val month = currentMonthItem + startMonth
if (month == startMonth) {
//重新设置日
setReDay(year_num, month, startDay, 31, list_big, list_little)
} else {
//重新设置日
setReDay(year_num, month, 1, 31, list_big, list_little)
}
} else if (year_num == endYear) {
//重新设置月份
wv_month2!!.adapter = NumericWheelAdapter(1, endMonth)
if (currentMonthItem > wv_month2!!.adapter.itemsCount - 1) {
currentMonthItem = wv_month2!!.adapter.itemsCount - 1
wv_month2!!.currentItem = currentMonthItem
}
val monthNum = currentMonthItem + 1
if (monthNum == endMonth) {
//重新设置日
setReDay(year_num, monthNum, 1, endDay, list_big, list_little)
} else {
//重新设置日
setReDay(year_num, monthNum, 1, 31, list_big, list_little)
}
} else {
//重新设置月份
wv_month2!!.adapter = NumericWheelAdapter(1, 12)
//重新设置日
setReDay(year_num, wv_month2!!.currentItem + 1, 1, 31, list_big, list_little)
}
if (mSelectChangeCallback != null) {
mSelectChangeCallback!!.onTimeSelectChanged()
}
}
// 添加"月"监听
wv_month2!!.setOnItemSelectedListener { index ->
var month_num = index + 1
if (startYear == endYear) {
month_num = month_num + startMonth - 1
if (startMonth == endMonth) {
//重新设置日
setReDay(currentYear, month_num, startDay, endDay, list_big, list_little)
} else if (startMonth == month_num) {
//重新设置日
setReDay(currentYear, month_num, startDay, 31, list_big, list_little)
} else if (endMonth == month_num) {
setReDay(currentYear, month_num, 1, endDay, list_big, list_little)
} else {
setReDay(currentYear, month_num, 1, 31, list_big, list_little)
}
} else if (currentYear == startYear) {
month_num = month_num + startMonth - 1
if (month_num == startMonth) {
//重新设置日
setReDay(currentYear, month_num, startDay, 31, list_big, list_little)
} else {
//重新设置日
setReDay(currentYear, month_num, 1, 31, list_big, list_little)
}
} else if (currentYear == endYear) {
if (month_num == endMonth) {
//重新设置日
setReDay(
currentYear,
wv_month2!!.currentItem + 1,
1,
endDay,
list_big,
list_little
)
} else {
setReDay(currentYear, wv_month2!!.currentItem + 1, 1, 31, list_big, list_little)
}
} else {
//重新设置日
setReDay(currentYear, month_num, 1, 31, list_big, list_little)
}
if (mSelectChangeCallback != null) {
mSelectChangeCallback!!.onTimeSelectChanged()
}
}
setChangedListener(wv_day2)
require(type.size == 6) { "type[] length is not 6" }
wv_year2!!.visibility = if (type[0]) View.VISIBLE else View.GONE
wv_month2!!.visibility = if (type[1]) View.VISIBLE else View.GONE
wv_day2!!.visibility = if (type[2]) View.VISIBLE else View.GONE
}
private fun setChangedListener(wheelView: WheelView?) {
if (mSelectChangeCallback != null) {
wheelView!!.setOnItemSelectedListener { mSelectChangeCallback!!.onTimeSelectChanged() }
}
}
private fun setReDay(
year_num: Int,
monthNum: Int,
startD: Int,
endD: Int,
list_big: List<String>,
list_little: List<String>
) {
var endD = endD
var currentItem = wv_day!!.currentItem
// int maxItem;
if (list_big.contains(monthNum.toString())) {
if (endD > 31) {
endD = 31
}
wv_day!!.adapter = NumericWheelAdapter(startD, endD)
// maxItem = endD;
} else if (list_little.contains(monthNum.toString())) {
if (endD > 30) {
endD = 30
}
wv_day!!.adapter = NumericWheelAdapter(startD, endD)
// maxItem = endD;
} else {
if (year_num % 4 == 0 && year_num % 100 != 0
|| year_num % 400 == 0
) {
if (endD > 29) {
endD = 29
}
wv_day!!.adapter = NumericWheelAdapter(startD, endD)
// maxItem = endD;
} else {
if (endD > 28) {
endD = 28
}
wv_day!!.adapter = NumericWheelAdapter(startD, endD)
// maxItem = endD;
}
}
if (currentItem > wv_day!!.adapter.itemsCount - 1) {
currentItem = wv_day!!.adapter.itemsCount - 1
wv_day!!.currentItem = currentItem
}
}
private fun setContentTextSize() {
wv_day!!.setTextSize(textSize.toFloat())
wv_month!!.setTextSize(textSize.toFloat())
wv_year!!.setTextSize(textSize.toFloat())
wv_day2!!.setTextSize(textSize.toFloat())
wv_month2!!.setTextSize(textSize.toFloat())
wv_year2!!.setTextSize(textSize.toFloat())
}
fun setLabels(label_year: String?, label_month: String?, label_day: String?) {
if (label_year != null) {
wv_year!!.setLabel(label_year)
wv_year2!!.setLabel(label_year)
} else {
wv_year!!.setLabel("年")
wv_year2!!.setLabel("年")
}
if (label_month != null) {
wv_month!!.setLabel(label_month)
wv_month2!!.setLabel(label_month)
} else {
wv_month!!.setLabel("月")
wv_month2!!.setLabel("月")
}
if (label_day != null) {
wv_day!!.setLabel(label_day)
wv_day2!!.setLabel(label_day)
} else {
wv_day!!.setLabel("日")
wv_day2!!.setLabel("日")
}
}
fun setTextXOffset(x_offset_year: Int, x_offset_month: Int, x_offset_day: Int) {
wv_year!!.setTextXOffset(x_offset_year)
wv_month!!.setTextXOffset(x_offset_month)
wv_day!!.setTextXOffset(x_offset_day)
wv_year2!!.setTextXOffset(x_offset_year)
wv_month2!!.setTextXOffset(x_offset_month)
wv_day2!!.setTextXOffset(x_offset_day)
}
/**
* 设置是否循环滚动
*
* @param cyclic
*/
fun setCyclic(cyclic: Boolean) {
wv_year!!.setCyclic(cyclic)
wv_month!!.setCyclic(cyclic)
wv_day!!.setCyclic(cyclic)
wv_year2!!.setCyclic(cyclic)
wv_month2!!.setCyclic(cyclic)
wv_day2!!.setCyclic(cyclic)
}
val time: String
get() {
val sb = StringBuilder()
if (currentYear == startYear) {
if (wv_month!!.currentItem + startMonth == startMonth) {
sb.append(wv_year!!.currentItem + startYear).append("-")
.append(wv_month!!.currentItem + startMonth).append("-")
.append(wv_day!!.currentItem + startDay)
} else {
sb.append(wv_year!!.currentItem + startYear).append("-")
.append(wv_month!!.currentItem + startMonth).append("-")
.append(wv_day!!.currentItem + 1)
}
} else {
sb.append(wv_year!!.currentItem + startYear).append("-")
.append(wv_month!!.currentItem + 1).append("-")
.append(wv_day!!.currentItem + 1)
}
return sb.toString()
}
val time2: String
get() {
val sb = StringBuilder()
if (currentYear == startYear) {
if (wv_month2!!.currentItem + startMonth == startMonth) {
sb.append(wv_year2!!.currentItem + startYear).append("-")
.append(wv_month2!!.currentItem + startMonth).append("-")
.append(wv_day2!!.currentItem + startDay)
} else {
sb.append(wv_year2!!.currentItem + startYear).append("-")
.append(wv_month2!!.currentItem + startMonth).append("-")
.append(wv_day2!!.currentItem + 1)
}
} else {
sb.append(wv_year2!!.currentItem + startYear).append("-")
.append(wv_month2!!.currentItem + 1).append("-")
.append(wv_day2!!.currentItem + 1)
}
return sb.toString()
}
/**
* 设置间距倍数,但是只能在1.0-4.0f之间
*
* @param lineSpacingMultiplier
*/
fun setLineSpacingMultiplier(lineSpacingMultiplier: Float) {
wv_day!!.setLineSpacingMultiplier(lineSpacingMultiplier)
wv_month!!.setLineSpacingMultiplier(lineSpacingMultiplier)
wv_year!!.setLineSpacingMultiplier(lineSpacingMultiplier)
wv_day2!!.setLineSpacingMultiplier(lineSpacingMultiplier)
wv_month2!!.setLineSpacingMultiplier(lineSpacingMultiplier)
wv_year2!!.setLineSpacingMultiplier(lineSpacingMultiplier)
}
/**
* 设置分割线的颜色
*
* @param dividerColor
*/
fun setDividerColor(dividerColor: Int) {
wv_day!!.setDividerColor(dividerColor)
wv_month!!.setDividerColor(dividerColor)
wv_year!!.setDividerColor(dividerColor)
wv_day2!!.setDividerColor(dividerColor)
wv_month2!!.setDividerColor(dividerColor)
wv_year2!!.setDividerColor(dividerColor)
}
/**
* 设置分割线的类型
*
* @param dividerType
*/
fun setDividerType(dividerType: DividerType?) {
wv_day!!.setDividerType(dividerType)
wv_month!!.setDividerType(dividerType)
wv_year!!.setDividerType(dividerType)
wv_day2!!.setDividerType(dividerType)
wv_month2!!.setDividerType(dividerType)
wv_year2!!.setDividerType(dividerType)
}
/**
* 设置分割线之间的文字的颜色
*
* @param textColorCenter
*/
fun setTextColorCenter(textColorCenter: Int) {
wv_day!!.setTextColorCenter(textColorCenter)
wv_month!!.setTextColorCenter(textColorCenter)
wv_year!!.setTextColorCenter(textColorCenter)
wv_day2!!.setTextColorCenter(textColorCenter)
wv_month2!!.setTextColorCenter(textColorCenter)
wv_year2!!.setTextColorCenter(textColorCenter)
}
/**
* 设置分割线以外文字的颜色
*
* @param textColorOut
*/
fun setTextColorOut(textColorOut: Int) {
wv_day!!.setTextColorOut(textColorOut)
wv_month!!.setTextColorOut(textColorOut)
wv_year!!.setTextColorOut(textColorOut)
wv_day2!!.setTextColorOut(textColorOut)
wv_month2!!.setTextColorOut(textColorOut)
wv_year2!!.setTextColorOut(textColorOut)
}
/**
* @param isCenterLabel 是否只显示中间选中项的
*/
fun isCenterLabel(isCenterLabel: Boolean) {
wv_day!!.isCenterLabel(isCenterLabel)
wv_month!!.isCenterLabel(isCenterLabel)
wv_year!!.isCenterLabel(isCenterLabel)
wv_day2!!.isCenterLabel(isCenterLabel)
wv_month2!!.isCenterLabel(isCenterLabel)
wv_year2!!.isCenterLabel(isCenterLabel)
}
fun setSelectChangeCallback(mSelectChangeCallback: ISelectTimeCallback?) {
this.mSelectChangeCallback = mSelectChangeCallback
}
fun setItemsVisible(itemsVisibleCount: Int) {
wv_day!!.setItemsVisibleCount(itemsVisibleCount)
wv_month!!.setItemsVisibleCount(itemsVisibleCount)
wv_year!!.setItemsVisibleCount(itemsVisibleCount)
wv_day2!!.setItemsVisibleCount(itemsVisibleCount)
wv_month2!!.setItemsVisibleCount(itemsVisibleCount)
wv_year2!!.setItemsVisibleCount(itemsVisibleCount)
}
fun setAlphaGradient(isAlphaGradient: Boolean) {
wv_day!!.setAlphaGradient(isAlphaGradient)
wv_month!!.setAlphaGradient(isAlphaGradient)
wv_year!!.setAlphaGradient(isAlphaGradient)
wv_day2!!.setAlphaGradient(isAlphaGradient)
wv_month2!!.setAlphaGradient(isAlphaGradient)
wv_year2!!.setAlphaGradient(isAlphaGradient)
}
fun setRangDate(startDate: Calendar?, endDate: Calendar?) {
if (startDate == null && endDate != null) {
val year = endDate[Calendar.YEAR]
val month = endDate[Calendar.MONTH] + 1
val day = endDate[Calendar.DAY_OF_MONTH]
if (year > startYear) {
endYear = year
endMonth = month
endDay = day
} else if (year == startYear) {
if (month > startMonth) {
endYear = year
endMonth = month
endDay = day
} else if (month == startMonth) {
if (day > startDay) {
endYear = year
endMonth = month
endDay = day
}
}
}
} else if (startDate != null && endDate == null) {
val year = startDate[Calendar.YEAR]
val month = startDate[Calendar.MONTH] + 1
val day = startDate[Calendar.DAY_OF_MONTH]
if (year < endYear) {
startMonth = month
startDay = day
startYear = year
} else if (year == endYear) {
if (month < endMonth) {
startMonth = month
startDay = day
startYear = year
} else if (month == endMonth) {
if (day < endDay) {
startMonth = month
startDay = day
startYear = year
}
}
}
} else if (startDate != null && endDate != null) {
startYear = startDate[Calendar.YEAR]
endYear = endDate[Calendar.YEAR]
startMonth = startDate[Calendar.MONTH] + 1
endMonth = endDate[Calendar.MONTH] + 1
startDay = startDate[Calendar.DAY_OF_MONTH]
endDay = endDate[Calendar.DAY_OF_MONTH]
}
}
companion object {
var dateFormat: DateFormat = SimpleDateFormat("yyyy-MM-dd")
private const val DEFAULT_START_YEAR = 1900
private const val DEFAULT_END_YEAR = 2100
private const val DEFAULT_START_MONTH = 1
private const val DEFAULT_END_MONTH = 12
private const val DEFAULT_START_DAY = 1
private const val DEFAULT_END_DAY = 31
}
}
2.改造布局pickerview_time2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rv_topbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/pickerview_bg_topbar">
<Button
android:id="@+id/btnCancel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:gravity="left|center_vertical"
android:paddingLeft="@dimen/pickerview_topbar_padding"
android:text="@string/pickerview_cancel"
android:textAllCaps="false"
android:textColor="@drawable/selector_pickerview_btn"
android:textSize="@dimen/pickerview_topbar_btn_textsize" />
<TextView
android:id="@+id/tvTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btnSubmit"
android:layout_toRightOf="@+id/btnCancel"
android:gravity="center"
android:textColor="@color/pickerview_topbar_title"
android:textSize="@dimen/pickerview_topbar_title_textsize" />
<Button
android:id="@id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:background="@android:color/transparent"
android:gravity="right|center_vertical"
android:paddingRight="@dimen/pickerview_topbar_padding"
android:text="@string/pickerview_submit"
android:textAllCaps="false"
android:textColor="@drawable/selector_pickerview_btn"
android:textSize="@dimen/pickerview_topbar_btn_textsize" />
</RelativeLayout>
<LinearLayout
android:padding="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/timepicker"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:minHeight="180dp"
android:orientation="horizontal">
<com.contrarywind.view.WheelView
android:id="@+id/year"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.1" />
<com.contrarywind.view.WheelView
android:id="@+id/month"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.contrarywind.view.WheelView
android:id="@+id/day"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<View
android:background="#cccccc"
android:layout_width="0.5dp"
android:layout_height="180dp"
/>
<LinearLayout
android:id="@+id/timepicker2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="@android:color/white"
android:gravity="center"
android:minHeight="180dp"
android:orientation="horizontal">
<com.contrarywind.view.WheelView
android:id="@+id/year2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.1" />
<com.contrarywind.view.WheelView
android:id="@+id/month2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.contrarywind.view.WheelView
android:id="@+id/day2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
3.使用:调用showPicker()
方法即可
private fun showPicker(){
val selectedDate = Calendar.getInstance()
val startDate = Calendar.getInstance()
val endDate = Calendar.getInstance()
startDate.set(2013,0,1)
endDate.set(2099,11,31)
pvTime = TimePickerBuilder2(this) { date, date2 -> //选中事件回调
Log.e(TAG,getTime(date)+"到"+getTime(date2))
}
.setType(booleanArrayOf(true, true, true, false, false, false)) // 默认全部显示
.setCancelText("取消") //取消按钮文字
.setSubmitText("确定") //确认按钮文字
.setContentTextSize(18) //滚轮文字大小
.setTitleSize(20) //标题文字大小
.setTitleText("开始结束时间选择") //标题文字
.setOutSideCancelable(false) //点击屏幕,点在控件外部范围时,是否取消显示
.isCyclic(false) //是否循环滚动
.setDate(selectedDate) // 如果不设置的话,默认是系统时间*/
.setRangDate(startDate, endDate) //起始终止年月日设定
.setLabel("年", "月", "日") //默认设置为年月日时分秒
.isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。
.isDialog(true) //是否显示为对话框样式
.build()
pvTime.show()
}
网友评论