实现效果展示:
使用的第三方:https://github.com/Bigkoo/Android-PickerView
封装工具类:
var pvTime: TimePickerView? = null
fun showTimePickerDialog(
context: Context,
title: String,
types: BooleanArray,
curDate: Calendar,
startDate: Calendar,
endDate: Calendar,
listener: OnTimeSelectListener
) {
var mTypes = types
if (types == null)
mTypes = booleanArrayOf(true, true, true, true, true, true)
pvTime = TimePickerBuilder(
context,
listener
)
.setType(mTypes)
.setCancelColor(context.resources.getColor(R.color.ty_theme_color_b2_n3))
.setCancelText(context.resources.getString(R.string.ty_cancel))
.setSubmitColor(context.resources.getColor(R.color.ty_theme_color_b2_n1))
.setSubmitText(context.resources.getString(R.string.ty_confirm))
.setSubCalSize(16)//确定和取消文字大小
// .setTitleText(title)
// .setTitleSize(16)
// .setTitleColor(context.resources.getColor(R.color.ty_theme_color_b2_n3))
.setContentTextSize(18)//滚轮文字大小
.setOutSideCancelable(true)
.setBgColor(context.resources.getColor(R.color.ty_theme_color_b1))
.setTitleBgColor(context.resources.getColor(R.color.ty_theme_color_b1))//标题背景颜色 Night mode
.setTextColorCenter(context.resources.getColor(R.color.ty_theme_color_b2_n1))
.setDate(curDate)
// .setRangDate(startDate, endDate)
.apply {
var country = context.resources.configuration.locale.language
if(country.equals("zh")){
setLabel("年","月","日","时","分","")
}else{
setLabel(" "," "," ","H","M","")
}
}
.build()
val mDialog = pvTime?.dialog
if (mDialog != null) {
val params = FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
450,
Gravity.BOTTOM
)
params.leftMargin = 0
params.rightMargin = 0
pvTime?.dialogContainerLayout?.layoutParams = params
val dialogWindow = mDialog.window
if (dialogWindow != null) {
dialogWindow.setWindowAnimations(R.style.picker_view_slide_anim) //修改动画样式
dialogWindow.setGravity(Gravity.BOTTOM) //改成Bottom,底部显示
}
}
pvTime?.show()
}
使用示例:
fun selectStartTime(
curTime: Long,
authEndTime: Long,
onSuccess: (authTime: AuthTime) -> Unit = {},
onFailure: (msg: String) -> Unit = {}
) {
val types = booleanArrayOf(true, true, true, true, true, false)
var curDate = Calendar.getInstance()
if(curTime != 0L)
curDate.timeInMillis = curTime
var startDate = Calendar.getInstance().transferDate()
var endDate = Calendar.getInstance().transferDate()
endDate.add(Calendar.YEAR, 60)
showTimePickerDialog(context, getString(R.string.wb_app_securityMonitor_authorize_authorize_start_time), types,curDate, startDate, endDate,
OnTimeSelectListener { date, v ->
if (authEndTime != 0L && date.time > authEndTime) {
onFailure.invoke(getString(R.string.wb_app_securityMonitor_authorize_start_time_error_hint))
return@OnTimeSelectListener
}
var timeStr = date.dateFormatToStr("yyyy-MM-dd HH:mm")
onSuccess.invoke(AuthTime(timeStr, date))
pvTime?.dismiss()
})
}
日期转换方法:
//获取当前日期的下一个整点
fun Calendar.transferDate(): Calendar {
this.let { instance ->
var ca = Calendar.getInstance()
ca.set(Calendar.SECOND, 0)
ca.set(Calendar.MILLISECOND, 0)
if (ca.time.time % 3600 == 0L) return this
ca.add(Calendar.HOUR, 1)
ca.set(Calendar.MINUTE, 0)
return ca
}
}
//日期格式化
fun Date.dateFormatToStr(pattern:String="yyyy-MM-dd HH:mm:ss"):String{
if(this == null ) return ""
var format = SimpleDateFormat(pattern)
return format.format(this)
}
//时间格式化
fun Long?.timeFormatToStr(pattern:String="yyyy-MM-dd HH:mm:ss"):String{
if(this == null || this == 0L) return ""
var format = SimpleDateFormat(pattern)
return format.format(Date(this))
}
获取今天0点及最后一秒时间:
var startTime = Date(System.currentTimeMillis())
var rawOffset = TimeZone.getDefault().rawOffset
val zero = (startTime+rawOffset) / (1000 * 3600 * 24) * (1000 * 3600 * 24) - rawOffset //今天零点零分零秒的毫秒数
val twelve = zero + 24 * 60 * 60 * 1000 - 1 //今天23点59分59秒的毫秒数
getRawOffset()
该方法以毫秒为单位返回需要添加到UTC以获得此TimeZone中的标准时间的时间。
必须在除数+getRawOffset()方法,否则可能会出现跨天的现象,导致结果是错误的。详情见:获取当天零时时间戳之误区