DateSelectHelper.kt 2.21 KB
Newer Older
gaodapeng's avatar
gaodapeng committed
1 2
package com.miya.fastcashier.utils

gaodapeng's avatar
gaodapeng committed
3
import android.app.Activity
gaodapeng's avatar
gaodapeng committed
4 5 6 7 8 9 10 11 12 13 14
import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import androidx.core.content.res.ResourcesCompat
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.bigkoo.pickerview.listener.OnTimeSelectListener
import com.bigkoo.pickerview.view.TimePickerView
import com.miya.fastcashier.R
import java.util.*

gaodapeng's avatar
gaodapeng committed
15
class DateSelectHelper(val act: Activity, val callback: (Date) -> Unit) {
gaodapeng's avatar
gaodapeng committed
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

    var timePickerView: TimePickerView? = null
    fun showDatePicker() {
        if (timePickerView == null) {
            initTimePicker()
        }
        val date = Calendar.getInstance()
        date.set(
            date.get(Calendar.YEAR),
            date.get(Calendar.MONTH),
            date.get(Calendar.DAY_OF_MONTH),
            0,
            0
        )
        timePickerView!!.setDate(date)
        timePickerView!!.show()
    }

    private fun initTimePicker() {
        val calendar = Calendar.getInstance()
        val startDate = Calendar.getInstance()
        val endDate = Calendar.getInstance()

        startDate.set(
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),
            0,
gaodapeng's avatar
gaodapeng committed
44
            0,
gaodapeng's avatar
gaodapeng committed
45 46 47 48 49 50 51 52 53
            0
        )
        endDate.set(
            calendar.get(Calendar.YEAR),
            calendar.get(Calendar.MONTH),
            calendar.get(Calendar.DAY_OF_MONTH),
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE)
        )
gaodapeng's avatar
gaodapeng committed
54
        timePickerView = TimePickerBuilder(act, OnTimeSelectListener { date, v ->
gaodapeng's avatar
gaodapeng committed
55 56 57 58 59
            callback(date)
        })
            .setTitleText("请选择开始时间")
            .setType(booleanArrayOf(false, false, false, true, true, false))
            .setDate(startDate)
gaodapeng's avatar
gaodapeng committed
60 61 62
            .setContentTextSize(30)
            .setTitleSize(30)
            .setSubCalSize(30)
zhaopengxiang's avatar
zhaopengxiang committed
63
            .setLineSpacingMultiplier(1.9f)
gaodapeng's avatar
gaodapeng committed
64
            .setRangDate(startDate, endDate)
gaodapeng's avatar
gaodapeng committed
65 66
            .setOutSideCancelable(false)
            .setDecorView(act.findViewById(android.R.id.content))
gaodapeng's avatar
gaodapeng committed
67 68 69 70 71 72 73 74 75
            .build()
    }

    fun dismiss() {
        timePickerView?.dismiss()
    }


}