如何在 Compose 函数的按钮单击中启动 Intent

时间:2021-04-04 10:09:33

标签: android kotlin android-jetpack-compose

我想将用户重定向到另一个打开互联网 URL 的活动。点击按钮。

这是我目前的代码

Button(onClick = {
          val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))
          // Here is some code that starts the activity
       }) {
          Text(text="APPLY HACK")
       }

1 个答案:

答案 0 :(得分:3)

您可以使用以下内容:

val intent = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"))
val context = LocalContext.current

Button(onClick = {
    startActivity(context, intent, null) }
) {
    Text("BUTTON")
}