Kotlin中的自定义对话框?

时间:2018-06-26 13:29:34

标签: android kotlin

我想在单击btnStart按钮后关闭一个对话框。我不想在对话框上应用否定和肯定按钮。

  val context = this
        val builder = AlertDialog.Builder(context)
        // Seems ok to inflate view with null rootView
        val view = layoutInflater.inflate(R.layout.activity_dialog, null)
        builder.setView(view)
        val btnStart=view.findViewById(R.id.btn_start_progress_dialog) as Button

        btnStart.setOnClickListener(View.OnClickListener {
          /*
          * I want close dialog box from here.
          *
          * */

        })


        builder.setNegativeButton(android.R.string.cancel) { dialog, p1 ->
            dialog.cancel()
        }



        builder.show()

2 个答案:

答案 0 :(得分:0)

如果要隐藏它,只需将其设置为null

builder.setPositiveButton(null,null)
builder.setNegativeButton(null,null)

答案 1 :(得分:0)

在显示对话框参考之前,您需要使用create获取对话框参考。这样,可以很容易地附加回调以解雇:

// first create the dialog
val dialog = AlertDialog.Builder(context)
        .setView(view)
        .create()
// now we have a reference to dismiss it
btnStart.setOnClickListener {
    dialog.dismiss()
}
dialog.show()