Jetpack Compose 中的 OutlinedTextField,单击打开时间选取器并更新 OutlinedTextField

OutlinedTextField in jetpack compose on click open Time picker and update the OutlinedTextField

提问人:Kalpesh Shinde 提问时间:11/8/2023 最后编辑:Kalpesh Shinde 更新时间:11/8/2023 访问量:39

问:

android OutlinedTextField 单击并打开时间选取器 它只适用于标签文本单击如何在事件上单击整个 OutlinedTextField

OutlinedTextField(
                            value = selectedTimeState,
                            onValueChange = { selectedTimeState = it },
                            label = { Text("Selected Time") },
                            modifier = Modifier
                            .fillMaxWidth().clickable {
                                Toast.makeText(context, "click", Toast.LENGTH_SHORT).show()
                            },
                            readOnly = true,
                            trailingIcon = {
                                Spacer(modifier = Modifier.width(8.dp))
                                Text(
                                    text = AnnotatedString(text = lateTimeState),
                                    style = MaterialTheme.typography.bodySmall
                                )
                                Spacer(modifier = Modifier.width(8.dp))
                            },
                        )

enter image description here

android OutlinedTextField 单击并打开时间选取器

安卓 android-jetpack-compose 喷气背包

评论

0赞 JanItor 11/8/2023
这回答了你的问题吗?Jetpack Compose:TextField 可点击不起作用
0赞 Vivek Gupta 11/9/2023
是否要在单击 outlineTextfield 的任何部分时打开计时器对话框?

答:

0赞 Hanif Shaikh 11/8/2023 #1
val datePicker = DatePickerDialog(
        context, { _: DatePicker, selectedYear: Int, selectedMonth: Int, selectedDayOfMonth: Int ->
   
            calendar[Calendar.YEAR] = selectedYear
            calendar[Calendar.MONTH] = selectedMonth
            calendar[Calendar.DAY_OF_MONTH] = selectedDayOfMonth
            if (isDOB) {
                selectedDateText = formatterDate(
                    Date(calendar.timeInMillis).toString(),
                    "EEE MMM dd HH:mm:ss zzz yyyy",
                    "yyyy-MM-dd"
                )!!
                onValueChange(selectedDateText)
            } else {
                val convertingInUTCFromCalendar = formatterDate(
                    Date(calendar.timeInMillis).toString(),
                    "EEE MMM dd HH:mm:ss zzz yyyy",
                    "yyyy-MM-dd'T'HH:mm:ss.SSS"
                )!!
                val parsed: DateTime =
                    DateTime(convertingInUTCFromCalendar).withZone(DateTimeZone.UTC)

                val myUTCFormat = formatterDate(
                    parsed.toString(),
                    "yyyy-MM-dd'T'HH:mm:ss.SSS",
                    "yyyy-MM-dd HH:mm:ss"
                )

                selectedDateText = myUTCFormat!!
                onValueChange(selectedDateText)
            }
        }, calendar[Calendar.YEAR], calendar[Calendar.MONTH], calendar[Calendar.DAY_OF_MONTH]
    )


    OutlinedTextField(
        value = selectedDateText,
        onValueChange = { onValueChange(it) },
        readOnly = true,
        modifier = modifier
            .fillMaxWidth()
            .wrapContentHeight(),
        keyboardOptions = KeyboardOptions(
            capitalization = KeyboardCapitalization.Characters,
            autoCorrect = false,
        ),
        placeholder = {
            Text(
                text = "dd/mm/yyyy",
                fontWeight = FontWeight(400),
                fontSize = 14.sp,
                color = RomanSilver,
            )
        },
        trailingIcon = {
            IconButton(
                onClick = { datePicker.show() },
            ) {
                Icon(
                    painter = painterResource(id = R.drawable.calendar),
                    contentDescription = null,
                    tint = RomanSilver
                )
            }
        },
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = MaterialTheme.colors.secondaryVariant,
            cursorColor = RomanSilver,
            disabledLabelColor = RomanSilver,
            focusedIndicatorColor = Color.Transparent,
            unfocusedIndicatorColor = Color.Transparent,
            textColor = LocalCustomColorsPalette.current.textColor,
            disabledTextColor = LocalCustomColorsPalette.current.textColor,
            disabledIndicatorColor = Color.Transparent
        ),
        shape = (RoundedCornerShape(40)),
        isError = isError,
        keyboardActions = KeyboardActions(onDone = { focusManager.clearFocus() }),
        interactionSource = interactionSource
    )

使用上面的代码,我实现了同样的事情,这仅供您参考,因为我已经根据我的要求创建了自己的可组合函数,如果您不理解许多人的想法,请随时在评论中提问。