5.15
在网上找了一个jetpack compose 的 日期时间选择器
但是还不知到 怎么将里面的时间提取出来
```
@Preview
@Composable
fun DataTimePicker(
viewModel: TimeViewModel = viewModel(factory = TimeViewModelProvider.Factory),
date: Date = Date()
) {
val itemHeight = 50.dp
Box(
modifier = Modifier
.wrapContentHeight()
.fillMaxWidth(),
Alignment.Center
) {
Row(
Modifier
.wrapContentHeight()
// .fillMaxWidth()
.background(MaterialTheme.colorScheme.surface),
Arrangement.SpaceEvenly,
Alignment.CenterVertically
) {
val year = date.getYearr()
val selectYear = rememberSaveable { mutableStateOf(year) }
val years = LinkedList<Pair<Int, String>>().apply {
for (i in year downTo 1980) {
add(Pair(i, "${i}年"))
}
}
DatePickerColumn(years, itemHeight, 55.dp, selectYear)
// 月份
val month = date.getMonthh()
val selectMonth = rememberSaveable { mutableStateOf(month) }
val months = ArrayList<Pair<Int, String>>(12).apply {
for (i in 1..12) {
add(Pair(i, "${i}月"))
}
}
DatePickerColumn(months, itemHeight, 40.dp, selectMonth)
// 月份的天数
val dayOfMon = date.getDayOfMonth()
val selectDay = rememberSaveable { mutableStateOf(dayOfMon) }
val dayCountOfMonth =
DateUtil.getDayCountOfMonth(selectYear.value, selectMonth.value)
// 提前定义好
val day31 = ArrayList<Pair<Int, String>>().apply {
for (i in 1..31)
add(Pair(i, "${i}日"))
}
val day30 = ArrayList<Pair<Int, String>>().apply {
for (i in 1..30)
add(Pair(i, "${i}日"))
}
val day29 = ArrayList<Pair<Int, String>>().apply {
for (i in 1..29)
add(Pair(i, "${i}日"))
}
val day28 = ArrayList<Pair<Int, String>>().apply {
for (i in 1..28)
add(Pair(i, "${i}日"))
}
// 快速切换
val dayOfMonList = when (dayCountOfMonth) {
28 -> day28
29 -> day29
30 -> day30
else -> day31
}
DatePickerColumn(
pairList = dayOfMonList,
itemHeight = itemHeight,
valueState = selectDay
)
// 小时
val hour = date.getHour()
val selectHour = rememberSaveable { mutableStateOf(hour) }
val hours = ArrayList<Pair<Int, String>>(24).apply {
for (i in 0..23) {
add(Pair(i, "${i}时"))
}
}
DatePickerColumn(hours, itemHeight, 40.dp, selectHour)
// 分
val minute = date.getMinute()
val selectMinute = rememberSaveable { mutableStateOf(minute) }
val minutes = ArrayList<Pair<Int, String>>(60).apply {
for (i in 0..59) {
add(Pair(i, "${i}分"))
}
}
DatePickerColumn(minutes, itemHeight, 40.dp, selectMinute)
// 秒
val second = date.getSecond()
val selectSecond = rememberSaveable { mutableStateOf(second) }
val seconds = ArrayList<Pair<Int, String>>(60).apply {
for (i in 0..59) {
add(Pair(i, "${i}秒"))
}
}
DatePickerColumn(seconds, itemHeight, 40.dp, selectSecond)
}
}
```