[英]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.
不能使用提供的参数调用以下函数。
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.唯一必须正确声明的是
value
和onValueChange
因为它们是唯一没有声明默认值的两个。
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)) }
)
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)) } )
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.