如何使用 Jetpack Compose 的 Material TextField Composable 的 readOnly 属性

How to use the readOnly property of the Material TextField Composable with Jetpack Compose

我的用例是我有一个表单,其中一些字段是手动填写的,其他字段将用户带到一个屏幕,他们可以在该屏幕上在一个大列表中搜索他们想要的值。仅接受用户输入的字段工作正常,但是当我尝试将 属性 readOnly = true 添加到 TextField 可组合项时,出现以下错误:

None of the following functions can be called with the arguments supplied.
TextField(TextFieldValue, (TextFieldValue) → Unit, Modifier = ..., TextStyle = ..., (() → Unit)? = ..., (() → Unit)? = ..., (() → Unit)? = ..., (() → Unit)? = ..., Boolean = ..., VisualTransformation = ..., KeyboardOptions = ..., Boolean = ..., Int = ..., (ImeAction, SoftwareKeyboardController?) → Unit = ..., (SoftwareKeyboardController) → Unit = ..., InteractionState = ..., Color = ..., Color = ..., Color = ..., Color = ..., Shape = ...) defined in androidx.compose.material
TextField(String, (String) → Unit, Modifier = ..., TextStyle = ..., (() → Unit)? = ..., (() → Unit)? = ..., (() → Unit)? = ..., (() → Unit)? = ..., Boolean = ..., VisualTransformation = ..., KeyboardOptions = ..., Boolean = ..., Int = ..., (ImeAction, SoftwareKeyboardController?) → Unit = ..., (SoftwareKeyboardController) → Unit = ..., InteractionState = ..., Color = ..., Color = ..., Color = ..., Color = ..., Shape = ...) defined in androidx.compose.material

我假设这个错误与我的参数不匹配 TextField 提供的覆盖有关,但我似乎无法在文档中找到任何关于我应该如何使用它的内容属性 而不是简单地为其提供 Boolean 值。我正在寻找 here 文档。

密码是:

TextField(
   value = statefulValue,
   onValueChange = {},
   label = {
        Text("Label")
   },
   placeholder = {
       Text("Search items")
   },
   trailingIcon = {
       Icon(
           imageVector = Icons.Filled.Search
       )
   },
   readOnly = true
)

我正在使用 androidx.compose.material:material:1.0.0-alpha09 版本的 Compose Material 包,alpha09 也用于其余的 compose 依赖项。

在将我所有的 compose 依赖项更新为 1.0.0-alpha10 之后,我能够使用 readOnly 属性,然后使缓存失效并重新启动 Android Studio。

我发现这个 属性 是在 alpha10 中引入的,所以这就是我之前收到错误的原因。

以下是我用来得出这个结论的链接: