我怎么能在这个对话框中启动另一个活动?

问题描述 投票:0回答:1

我想当我点击 "确定 "按钮时,我进入 "PickSeatActivity".这是我试过的,但android studio用红线在 "Intent "对象下划线。

        val builder = AlertDialog.Builder(this.requireActivity())

         // Dialog will have "Make a selection" as the title
         builder.setMessage("Details successfully captured.Do you wish to proceed and book your seat?")
             .setPositiveButton("OK") { dialogInterface: DialogInterface, i: Int ->
                 val intent = Intent(PickSeatActivity::class.java)
                 startActivity(intent)


             }
             // A "Cancel" button that does nothing
             .setNegativeButton("Cancel") { dialog, id ->
                 // Nothing happening here either
             }
android kotlin dialog android-alertdialog
1个回答
3
投票

替换你的代码

val intent = Intent(PickSeatActivity::class.java)
             startActivity(intent)

TO

val intent = Intent(this.requireActivity(), PickSeatActivity::class.java)
             startActivity(intent)

2
投票

把你的代码改成这样

val intent = Intent(this@yourActivity, PickSeatActivity::class.java)
startActivity(intent)

val intent = Intent(this.requireActivity(), PickSeatActivity::class.java)
startActivity(intent)

1
投票

packageContext 是需要的。 public Intent(Context packageContext, Class<?> cls)

    val builder = AlertDialog.Builder(this.requireActivity())

     // Dialog will have "Make a selection" as the title
     builder.setMessage("Details successfully captured.Do you wish to proceed and book your seat?")
         .setPositiveButton("OK") { dialogInterface: DialogInterface, i: Int ->
             val intent = Intent(this.requireActivity(),PickSeatActivity::class.java)
             startActivity(intent)


         }
         // A "Cancel" button that does nothing
         .setNegativeButton("Cancel") { dialog, id ->
             // Nothing happening here either
         }
© www.soinside.com 2019 - 2024. All rights reserved.