以下函数均不能使用提供的参数来调用 | @Composable 调用只能在 @Composable

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

我在jetpack组件中为

TextField
创建了一个单独的自定义组件,并导入了下面的库,但它抛出了如下图所示的错误

import androidx.compose.material.Text
import androidx.compose.material.TextField

但是我无法编译它,它抛出了错误

我的实现

import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.material.TextFieldDefaults
import androidx.compose.runtime.Composable
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.input.ImeAction

import androidx.compose.ui.tooling.preview.Preview


@ExperimentalComposeUiApi
@Composable
fun NoteInputText(
    modifier: Modifier = Modifier,
    text: String,
    label: String,
    maxLine: Int = 1,
    onTextChange: (String) -> Unit,
    onImeAction: () -> Unit = {},
) {
    val keyboardController = LocalSoftwareKeyboardController.current

    TextField(
        value = text,
        onValueChange = onTextChange,
        colors = TextFieldDefaults.textFieldColors(
            backgroundColor = Color.Transparent
        ),
        maxLine = maxLine,
        label = {
            Text(text = label)
        },
        keyboardOptions = KeyboardOptions.Default.copy(
            imeAction = ImeAction.Done,
        ),
        keyboardActions = KeyboardActions(onDone = {
            onImeAction()
            keyboardController?.hide()
        }),
        modifier = modifier
    )
}

@ExperimentalComposeUiApi
@Preview(showBackground = true)
@Composable
fun Preview() {
    NoteInputText(text = "hello", label = "nam", onTextChange = {})
}

我遇到的错误

android kotlin android-jetpack-compose android-jetpack
1个回答
2
投票

尝试

maxLine: Int = 1,
maxLines: Int = 1, 

命名错误,TextField 中没有
maxLine

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