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

Pha*_*tom 3 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")
       }
Run Code Online (Sandbox Code Playgroud)

Gab*_*tti 5

您可以使用以下内容:

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

Button(onClick = {
    startActivity(context, intent, null) }
) {
    Text("BUTTON")
}
Run Code Online (Sandbox Code Playgroud)

  • 这个答案并不完整。它仅在您处于活动状态时才起作用。不在单独的可组合函数中 (9认同)