我有以下需求:用户可以在对话框的第一个文本字段中输入名称,但是当用户单击第二个(已禁用)文本字段时,应该打开一个DatePicker。
我已经按照SO上的一些说明实现了一个日期选择器,但是我遇到了一个奇怪的UI错误,与CalendarView下面的按钮有关。
这是我的Compose代码,包括CustomCalendarView:
问题是:
![](https://istack.dev59.com/fcWaB.webp)
这是我的Compose代码,包括CustomCalendarView:
@Composable
fun JourneyDatePicker(
isVisible: Boolean = false,
date: Long?,
onDateSelected: (Long) -> Unit,
onDismissRequest: () -> Unit
) {
var selectedDate by remember {
mutableStateOf(
when {
date != null && date > 0L -> date
else -> DateTime.now().millis
}
)
}
if (isVisible) Dialog(
onDismissRequest = onDismissRequest,
properties = DialogProperties()
) {
Column(
modifier = Modifier
.wrapContentSize()
.background(
shape = RoundedCornerShape(size = 16.dp)
)
) {
Column(
Modifier
.defaultMinSize(minHeight = 72.dp)
.fillMaxWidth()
.background(
shape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp)
)
.padding(16.dp)
) {
Text(
text = "Datum auswählen"
)
Spacer(modifier = Modifier.size(24.dp))
Text(
text = DateTime().withMillis(selectedDate).toString(DateTimeFormat.longDate())
)
Spacer(modifier = Modifier.size(16.dp))
}
CustomCalendarView(
date = selectedDate,
onDateSelected = {
selectedDate = it
}
)
Spacer(modifier = Modifier.size(8.dp))
Row(
modifier = Modifier
.align(Alignment.End)
.padding(bottom = 16.dp, end = 16.dp)
) {
TextButton(
onClick = {
selectedDate = DateTime.now().millis
onDismissRequest()
}
) {
Text(
text = "Cancel",
color = Color.Black
)
}
TextButton(
onClick = {
onDateSelected(selectedDate)
onDismissRequest()
}
) {
Text(
text = "OK",
color = Color.Black
)
}
}
}
}
}
@Composable
fun CustomCalendarView(
date: Long?,
onDateSelected: (Long) -> Unit
) {
AndroidView(
modifier = Modifier.wrapContentSize(),
factory = { context ->
CalendarView(ContextThemeWrapper(context, R.style.DatePickerCustom))
},
update = { view ->
view.apply {
date?.let { d ->
if (d > 0L) setDate(d) else DateTime.now().millis
}
setOnDateChangeListener { _, year, month, dayOfMonth ->
onDateSelected(
DateTime
.now()
.withMonthOfYear(month + 1)
.withYear(year)
.withDayOfMonth(dayOfMonth)
.millis
)
}
}
}
)
}
问题是:
当打开包含名称和日期两个输入字段的对话框并单击日期字段时,DatePicker的按钮将显示:
当对话框打开并编辑名称后,再单击日期字段,则不会显示按钮:
我已经使用FocusManager清除了焦点,以防这是键盘问题,但也没有帮助。
非常感谢任何帮助 :)