6

我开始使用 Jetpack compose (1.0.0-beta07),但在 TextField 上遇到了一个非常奇怪的问题。根据所有可能的文档和说明,我做的一切都是正确的,但 Android Studio 不断给我写None of the following functions can be called with the arguments supplied.消息TextField

下面是我编写的代码,其中 Studio 仍然在和 下划线Text (label)text = it但我认为它在定义TextField. 当我替换为 时问题消失remember {mutableStateOf ("text")}"text"TextField在键入键盘时不会更改文本。

import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.text.input.KeyboardType

@Composable
fun SimpleTextField(label: String = "Label", key: String = "unknown", keyboardType: KeyboardType = KeyboardType.Text){
    var text = remember {
        mutableStateOf("text")
    }

    TextField(
        value = text,
        onValueChange = {
            text = it
        },
        label = { Text(label) },
        keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
    )
}

错误图片

4

1 回答 1

2

您可以使用:

var text = remember { mutableStateOf("text") }

TextField(
    value = text.value,
    onValueChange = {
        text.value = it
    },
    label = { Text(label) },
    keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
)

或者:

var text by remember { mutableStateOf("text") }

TextField(
    value = text,
    onValueChange = {
        text = it
    },
    label = { Text(label) },
    keyboardOptions = KeyboardOptions(keyboardType = keyboardType)
)

您可以在官方文档中阅读有关委托属性的更多信息。

于 2021-05-20T09:57:10.753 回答