Compose TimePicker
预览
Composable
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Button
import androidx.compose.material.ButtonDefaults
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TimePicker
import androidx.compose.material3.TimePickerLayoutType
import androidx.compose.material3.rememberTimePickerState
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.TextUnit
import androidx.compose.ui.unit.TextUnitType
import androidx.compose.ui.unit.dp
import java.util.Calendar
@OptIn(ExperimentalMaterial3Api::class)
@Composable
@Preview
fun TimePickerExample() {
val currentTime = Calendar.getInstance()
val state = rememberTimePickerState(
initialHour = currentTime.get(Calendar.HOUR_OF_DAY),
initialMinute = currentTime.get(Calendar.MINUTE),
is24Hour = true,
)
val hour = state.hour
val minute = state.minute
val selectedValue ="$hour:$minute"
Text(
selectedValue,
modifier = Modifier,
fontSize = TextUnit(18f, TextUnitType.Sp)
)
BasicAlertDialog(
modifier = Modifier, onDismissRequest = {},
content = {
Column(
modifier = Modifier
.background(
color = Color.White, shape = RoundedCornerShape(8.dp)
)
.padding(start = 10.dp, end = 10.dp, top = 5.dp)
) {
Row(verticalAlignment = Alignment.CenterVertically, modifier = Modifier.padding(bottom = 5.dp, start = 20.dp, end = 20.dp)) {
Text(
"选择时间",
modifier = Modifier.weight(1f),
fontSize = TextUnit(18f, TextUnitType.Sp)
)
Button(
onClick = {
state.hour
},
shape = RoundedCornerShape(90.dp),
colors = ButtonDefaults.buttonColors(backgroundColor = Color(0xff6750A4))
) {
Text("确认", color = Color.White)
}
}
TimePicker(state, modifier = Modifier.fillMaxWidth(), layoutType = TimePickerLayoutType.Vertical)
}
},
)
}
原文地址:https://blog.csdn.net/u012127961/article/details/144455663
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!