我是Jetpack Compose新手,我试图在一个按钮内实现一个函数,但是它会产生以下错误:
@Composable调用只能在mContext.startActivity中@Composable函数的上下文中发生(意图(mContext,MainScreen()::class.java))
@Composable
fun AdminAuth() {
Column(
modifier = Modifier
.fillMaxSize()
.padding(20.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
var password by rememberSaveable { mutableStateOf("") }
var passawordVisibility by remember { mutableStateOf(false) }
val icon = if (passawordVisibility)
painterResource(id = R.drawable.ic_visibility)
else
painterResource(id = R.drawable.ic_visibility_off)
Text(text = "Insira a senha do usuário Master:", fontSize = 15.sp)
OutlinedTextField(
value = password,
onValueChange = {
password = it
},
placeholder = { Text(text = "Senha") },
label = { Text(text = "Senha") },
trailingIcon = {
IconButton(onClick = {
passawordVisibility = !passawordVisibility
}) {
Icon(
painter = icon,
contentDescription = "Ícone de visibilidade"
)
}
},
keyboardOptions = KeyboardOptions(
keyboardType = KeyboardType.Password
),
visualTransformation = if (passawordVisibility)
VisualTransformation.None
else PasswordVisualTransformation()
)
val mContext = LocalContext.current
OutlinedButton(
onClick = {
if (password.equals("Abac@xi123")) {
mContext.startActivity(Intent(mContext, MainScreen()::class.java))
}
},
modifier = Modifier
.fillMaxWidth()
.padding(35.dp),
) {
Text(text = "Entrar")
}
}
}
@Composable
@Preview
fun AdminAuthPreview() {
Column(
modifier = Modifier
.fillMaxSize()
.background(Color.White),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
AdminAuth()
}
}发布于 2022-10-19 02:17:25
调用startActivity在onClick中没有任何问题,但是您应该而不是调用MainScreen's构造函数(假设它是一个Activity),您只需删除括号就可以了。
onClick = {
if (password.equals("Abac@xi123")) {
mContext.startActivity(Intent(mContext, MainScreen::class.java))
}
}但我怀疑MainScreen只是另一个@Composable屏幕,而不是您想要导航到的Activity,而您的错误是因为
不能在不可组合的函数中调用可组合的函数。
就像正在看到的编译错误一样。
我建议进一步阅读,尤其是观看实时编码,您将直接看到如何从可组合的屏幕导航到另一个屏幕。
发布于 2022-10-19 03:42:23
mContext.startActivity(Intent(mContext, MainScreen()::class.java))这是不正确的方式来改变组成。即使对于更改活动,即使MainScreen是一个活动,因为MainScreen(),它也应该是MainScreen
可组合函数就像挂起函数,需要您将它们作为lambda、函数参数、内部参数或getter调用。
@Target(
// function declarations
// @Composable fun Foo() { ... }
// lambda expressions
// val foo = @Composable { ... }
AnnotationTarget.FUNCTION,
// type declarations
// var foo: @Composable () -> Unit = { ... }
// parameter types
// foo: @Composable () -> Unit
AnnotationTarget.TYPE,
// composable types inside of type signatures
// foo: (@Composable () -> Unit) -> Unit
AnnotationTarget.TYPE_PARAMETER,
// composable property getters and setters
// val foo: Int @Composable get() { ... }
// var bar: Int
// @Composable get() { ... }
AnnotationTarget.PROPERTY_GETTER
)
annotation class Composable如果您希望使用撰写更改UI,则可以使用
条件组合或组合导航以导航到下一个UI或屏幕。
有条件的变更通常是由一个国家或一个国旗
var screenState by remember{mutableStateOf(Idle)}
OutlinedButton(
onClick = {
if (password.equals("Abac@xi123")) {
screenState = Success
}
},
modifier = Modifier
.fillMaxWidth()
.padding(35.dp),
) {
Text(text = "Entrar")
}
when(screenState) {
Idle -> // Show Composable for this state
Success -> // Show Composable when user enters valid password
else -> // Implement other states of UI
}或者使用回调来导航,而不是传递navigationController
在根导航中使用此回调
OutlinedButton(
onClick = {
if (password.equals("Abac@xi123")) {
navigateToSuccessScreen()
}
},
modifier = Modifier
.fillMaxWidth()
.padding(35.dp),
) {
Text(text = "Entrar")
}https://stackoverflow.com/questions/74118893
复制相似问题