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

时间:2021-01-15 21:40:26

标签: android-jetpack-compose

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

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
)

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

1 个答案:

答案 0 :(得分:1)

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

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

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