在警报对话框 Kotlin 中使用服装按钮而不是正负按钮

use costume button instead of positive and negative buttons in alert dialog Kotlin

我需要在对话框片段上显示一些自定义按钮,但警报对话框只有 .setPositiveButton 和 .setNegativeButton

我的警报对话框必须类似于

我做了什么::

private fun alertDialog() {
        val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
        builder.setIcon(R.drawable.warning)
        builder.setTitle("خروج از برنامه ")
        builder.setMessage("از برنامه خارج میشوید؟")
            .setCancelable(false)
            .setPositiveButton("بله",
                DialogInterface.OnClickListener { dialog, id -> System.exit(0) })
            .setNegativeButton("خیر",
                DialogInterface.OnClickListener { dialog, id -> dialog.cancel() })
        val alert: AlertDialog = builder.create()
        alert.show()

    }

网上有这样的东西:

fun withItems(view: View) {
        val items = arrayOf("red", "Orange", "Yellow", "Blue")
        val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
        with(builder)
        {
            setTitle("ارسال گزارش به صورت:")
            setItems(items) { dialog, which ->
                Toast.makeText(activity, items[which] + " is clicked", 
              Toast.LENGTH_SHORT).show()
            }
            show()
        }
    }

但没有一个像我想要的那样工作

如果我理解正确,您需要创建一个自定义布局并将其提供给您的 alertDialog,例如:

val builder: AlertDialog.Builder = AlertDialog.Builder(requireActivity())
val dialogView: View = LayoutInflater.from(context).inflate(R.layout.dialog, null, false)
val myFirstButton: Button = dialogView.findViewById(R.id.first_button)
val mySecondButton: Button = dialogView.findViewById(R.id.second_button)
myFirstButton.setOnClickListener { ... }
mySecondButton.setOnClickListener { ... }
builder.setView(dialogView)
    .setTitle("MyTitle")
    .create()
    .show()