简体   繁体   English

为什么使用 Jetpack Compose TextField 时出现错误?

[英]Why am I getting an error using Jetpack Compose TextField?

EDIT编辑
I am still trying to get this to work.我仍在努力让它发挥作用。 I now have and I get an error on the Text for the label.我现在有并且在标签的Text上出现错误。 I am inside a function marked with @Composable.我在一个用@Composable 标记的函数中。 And still have a similar issue with the TextField too.而且TextField也有类似的问题。

@Composable invocations can only happen from the context of a @Composable function @Composable 调用只能在 @Composable 函数的上下文中发生

TextField(
    value = "Text(text = \"\")",
    onValueChange = {  },
    label = { Text("Label") },// copied from android developer website
    Modifier
        .padding(0.dp)
        .padding(end = dimensionResource(id = R.dimen.child_edge_padding))
     )
}

END EDIT结束编辑
I'm trying to make a login screen for and I'm using TextField to allow the user to enter an email and password.我正在尝试制作一个登录屏幕,我正在使用TextField来允许用户输入电子邮件和密码。 I cant see anything wrong with what I have but I'm getting an error我看不出我的东西有什么问题,但我收到了一个错误

None of the following functions can be called with the arguments supplied.不能使用提供的参数调用以下函数。


Here is part of my code: 这是我的代码的一部分:
 Row(Modifier.padding(dimensionResource(id = R.dimen.container_edge_padding))) { Text( text = "${stringResource(id = R.string.email)}} : ", Modifier .padding(0.dp) // equivalent to padding inside .padding(end = dimensionResource(id = R.dimen.child_edge_padding)) // second padding acts as to margin putting space on the inside of the item ) TextField( value = "", placeholder = stringResource(id = R.string.login_email_hint), Modifier .padding(0.dp) .padding(end = dimensionResource(id = R.dimen.child_edge_padding)) ) }

When I hover over it this is what I see.当我将鼠标悬停在它上面时,这就是我所看到的。

在此处输入图片说明

Using the names of the parameters it shouldnt matter which parameters I use or what position they are relative to the declaration.使用参数的名称,我使用哪些参数或它们相对于声明的位置无关紧要。 If I change it like this, because the first 3 parameters are the value, placeholder, modifier it works.如果我像这样更改它,因为前 3 个参数是值、占位符、修饰符它起作用。 But both should work because that is how named parameters works.但两者都应该工作,因为这就是命名参数的工作方式。 Providing a default value allows for this.提供默认值允许这样做。 The only way I can get this working is by using the parameters in the order they are declared.我可以让它工作的唯一方法是按参数声明的顺序使用参数。 Which means if I want to use placeHolder I have to use the name of every parameter before it, in the order its declared, in order to use it.这意味着如果我想使用placeHolder我必须使用它之前的每个参数的名称,按照它声明的顺序,以便使用它。 The only ones that must be declared correctly are value and onValueChange because those are the only 2 that dont have a default declared.唯一必须正确声明的是valueonValueChange因为它们是唯一没有声明默认值的两个。

 TextField( value = "", onValueChange = {}, Modifier .padding(0.dp) .padding(end = dimensionResource(id = R.dimen.child_edge_padding)) )

placeholder parameter have a little bit another type that you try to use:占位符参数有一些您尝试使用的另一种类型:

 placeholder: @Composable (() -> Unit)? = null,

That the reason: it cannot use String instead of @Composable (() -> Unit)?那是因为它不能使用String而不是@Composable (() -> Unit)?

Try with the following code, it will help you.试试下面的代码,它会帮助你。

 TextField(
        value = "ab",
        onValueChange ={},
        modifier = Modifier
            .padding(0.dp)
            .padding(end = dimensionResource(id = R.dimen.child_edge_padding)),
        enabled = true,
        readOnly = false,
        textStyle = TextStyle.Default,
        placeholder = {stringResource(id = R.string.login_email_hint)},
        visualTransformation = VisualTransformation.None,
        keyboardOptions = KeyboardOptions.Default,
        keyboardActions = KeyboardActions(onDone = { }),
        maxLines = 1
    )

There are two variants of the TextField as pointed out by the highlighter.正如荧光笔所指出的, TextField有两种变体。 You, since you are providing the first parameter value as a string, are using the second variant.由于您将第一个参数值作为字符串提供,因此您正在使用第二个变体。 The second variant calls for a (String) -> Unit which is the onValueChange parameter.第二个变体需要一个(String) -> Unit ,它是onValueChange参数。 You are saying it yourself that this must be provided, and you yourself are not providing it in your code (Snippet 1).您自己说必须提供它,而您自己没有在代码中提供它(代码段 1)。 That it why it didn't match up with the variant这就是为什么它与变体不匹配的原因

I finally got it working by doing this like @Dharmender Manral said我终于像@Dharmender Manral 所说的那样通过这样做来实现它

TextField(
    value = "",
    onValueChange = {  },
    Modifier
        .padding(0.dp)
        .padding(start = dimensionResource(id = R.dimen.child_edge_padding)),
    enabled = true,
    readOnly = false,
    textStyle = LocalTextStyle.current,
    label = { Text("Label") },
    placeholder = { Text(text = stringResource(id = R.string.login_password_hint)) }
)

Then I was able to do this 然后我能够做到这一点
TextField( value = "", onValueChange = { }, Modifier .padding(0.dp) .padding(start = dimensionResource(id = R.dimen.child_edge_padding)), // enabled = true, // readOnly = false, // textStyle = LocalTextStyle.current, label = { Text("Label") }, placeholder = { Text(text = stringResource(id = R.string.login_password_hint)) } )

The only problem is the `Modifier` has a default parameter so it is optional and should be able to be placed in any order. 唯一的问题是 `Modifier` 有一个默认参数,所以它是可选的,应该能够以任何顺序放置。 One I got this working I was able to switch `placeHolder` and `label` around so `placeHolder` was first and it still worked like it should. 我得到了这个工作,我能够切换`placeHolder`和`label`,所以`placeHolder`是第一个,它仍然像它应该的那样工作。 I have a feeling this was a bug somewhere because as soon as I moved `modifier` to a different position it broke again. 我有一种感觉,这是某个地方的错误,因为一旦我将 `modifier` 移动到不同的位置,它就会再次崩溃。 But because `modifier` has a default parameter it is option and should be able to be moved to any spot like I was able to do with `placeholder` and `label`. 但是因为 `modifier` 有一个默认参数,它是一个选项,应该能够像我使用 `placeholder` 和 `label` 那样移动到任何位置。

So the only difference between what worked and what didnt is as long as the first 3 parameters are in the proper spot and in the correct order the other parameters work as you would expect default parameters to work.因此,有效和无效之间的唯一区别是,只要前 3 个参数位于正确的位置并以正确的顺序,其他参数的工作就像您期望的默认参数一样工作。 Having them in any position.让他们处于任何位置。 Not sure why it doesnt work with modifier .不知道为什么它不适用于modifier

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在使用 Jetpack Compose clickable-Modifier 时收到后端内部错误:“IR 降低错误期间出现异常”? - Why am I getting Backend Internal error: "Exception during IR lowering error" when using Jetpack Compose clickable-Modifier? 我尝试从 compose jetpack 示例中打开“Jetsurveysample”,但出现构建错误,尽管我使用的是最新的 Android Studio 4.2 - I have tried to open “Jetsurveysample” from compose jetpack samples, but getting a build error, although I am using latest Android Studio 4.2 为什么我无法在 jetpack compose 中使用带有 firebase 的移动身份验证? - Why I am not able to use mobile authentication with firebase in jetpack compose? Jetpack Compose:文本字段和 FAB 不使用全宽 - Jetpack Compose: Textfield and FAB not using full width Jetpack 上的文本字段侦听器撰写? - Textfield listener on Jetpack compose? Jetpack Compose 中的密集文本字段 - Dense TextField in Jetpack Compose 自定义文本字段 jetpack 撰写,我不知道如何处理文本字段 - custom textfield jetpack compose, I do not know how to do with Textfield 为什么我的 Jetpack Compose Text 不重组而 TextField 重组 - Why my Jetpack Compose Text does not recomposed while TextField does 运行 Jetpack Compose 示例时出错 - Getting error when running Jetpack Compose samples 如何在 Jetpack Compose 中创建自定义 TextField - How can I create custom TextField in the Jetpack Compose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM