AlertDialog 中的 OutlinedTextField 不可编辑 - Android Compose

问题描述 投票:0回答:1

我正在尝试使用单个 OutlinedTextField 创建一个简单的 AlertDialog,它接受一些值并在单击确认按钮时执行操作。无论我尝试什么,文本字段似乎都不可编辑,我不明白为什么。

@Composable
fun InputDialog(
    dialogTitle: String,
    description: String,
    value: String,
    show: MutableState<Boolean>,
    onConfirm: (String) -> Unit) {
    var textFieldValue = TextFieldValue(value)
    AlertDialog(
        onDismissRequest = {
            show.value = false
        },
        confirmButton = {
            TextButton(
                onClick = {
                    onConfirm(textFieldValue.text)
                }
            ) {
                Text("Save")
            }
        },
        dismissButton = {
            TextButton(
                onClick = {
                    show.value = false
                }
            ) {
                Text("Cancel")
            }
        },
        text = {
            Column {
                Text(description)
                OutlinedTextField(
                    enabled = true,
                    readOnly = false,
                    value = textFieldValue,
                    onValueChange = {
                        textFieldValue = it
                    },
                    label = {
                        Text(dialogTitle.toUpperCase(Locale.current))
                    },
                    placeholder = {
                        Text(dialogTitle)
                    })
            }
        })
}
android-jetpack-compose android-alertdialog textfield
1个回答
0
投票

问题是文本字段的值未定义为状态。

你有这个:

@Composable
fun InputDialog(
    dialogTitle: String,
    description: String,
    value: String,
    show: MutableState<Boolean>,
    onConfirm: (String) -> Unit) {
    var textFieldValue = TextFieldValue(value) /// <------ HERE

你应该将其转换为这样:

val textFieldValue = remember { mutableStateOf("")}

有关 Jetpack Compose 中状态的更多信息,您可以阅读此处

© www.soinside.com 2019 - 2024. All rights reserved.