【问题标题】:How can I handle the cancel and the ok button in the dialog如何处理对话框中的取消和确定按钮
【发布时间】:2020-06-01 14:17:29
【问题描述】:

当我点击确定按钮时我想要的是它做某事的对话框,和取消按钮一样

覆盖 fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

    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?")
        // An OK button that does nothing
        .setPositiveButton("OK") { dialog, id ->
            // Nothing happening here

        }
        // A "Cancel" button that does nothing
        .setNegativeButton("Cancel") { dialog, id ->
            // Nothing happening here either
        }

【问题讨论】:

    标签: kotlin android-dialog


    【解决方案1】:
     val builder = AlertDialog.Builder(this)
                .setMessage("Yes or No")
                .setPositiveButton("OK"){ dialogInterface: DialogInterface, i: Int ->
                        Toast.makeText(this,"Ok Pressed",Toast.LENGTH_LONG).show()
                        dialogInterface.dismiss()
                }
                .setNegativeButton("No"){ dialogInterface: DialogInterface, i: Int ->
                        Toast.makeText(this,"No Pressed", Toast.LENGTH_SHORT).show()
                        dialogInterface.dismiss()
                }
    

    DialogInterface.OnClickListener 有两个参数:-

    dialog -> 接收点击的对话框 i -> 点击项目的位置

    像上面的吐司一样编写你的执行逻辑,最后你可以使用可用的对话框来关闭。

    【讨论】:

    • 您不必致电dismiss。当按下任何按钮时,它会自动关闭对话框。
    • 好吧,是否可以使确定按钮功能并将我路由到另一个活动
    • @Tenfour04 感谢您的指出。我的意图是告诉我们可以使用它来调用对话框接口方法。
    • @Saliko 是的,这是可能的。您只需将启动活动的逻辑放在那里而不是 Toast。请查看链接:- developer.android.com/training/basics/firstapp/…
    • @Sandeep Kumar 感谢本教程。这就是我之后所做的,但我在 Intent 对象中遇到错误 .setPositiveButton("OK") { dialogInterface: DialogInterface, i: Int -> val intent = Intent(PickSeatActivity::class.java) //val intent = Intent(this,PickSeatActivity::class.java) startActivity(intent) }
    【解决方案2】:

    我把我的代码替换成了这个,结果成功了

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-10
      • 2019-04-27
      • 1970-01-01
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      相关资源
      最近更新 更多