无法在Jetpack Compose中创建按钮

3

我发现大部分互联网上的教程都是这样的:

@Composable
fun addButton() {
   Button(text = "I'm a Compose Button")
}

但是Android Studio给我一个错误:类型不匹配,需要:() -> Unit,找到:String。我不知道如何修复它。


2
我在互联网上的大多数教程中发现,要检查这些教程的日期。Jetpack Compose目前仍处于开发者预览阶段,这意味着API变化非常快。 - CommonsWare
6个回答

9

1

0
请使用以下代码。
Button(onClick {/* your onClick listener */}){
   Text("I'm a Compose Button")
}

0

刚接触 Compose 的人可以使用像这样简单的按钮

    @Composable
fun MyButton() {
    Column(
        modifier = Modifier.fillMaxWidth().fillMaxHeight(),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Button(
            onClick = {},
            modifier = Modifier.padding(all = Dp(10f)),
            enabled = true,
            border = BorderStroke(width = 1.dp, brush = SolidColor(Color.Blue))

        ) {
            Text(text = "I am a compose button", color = Color.White)
        }
    }
}

0

我知道这看起来不好,但你必须这样做。

                Button(onClick = {handle Click Action }) 
                     {
                    Text(text = "Your Button Text")
                }

-1

你没有使用正确的语法,

你应该使用这段代码:

    Button(onClick = {/*Handle click action */} ){
    Text(
        text = "Write the button text here"
    )
}

如果您想使用修饰符,请参考以下内容,
    Button(onClick = {/*Handle click action */}, modifier = Modifier.padding(16.dp)) {
    Text(
        text = "Write the button text here"
    )
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接