【问题标题】:use costume button instead of positive and negative buttons in alert dialog Kotlin在警报对话框 Kotlin 中使用服装按钮而不是正面和负面按钮
【发布时间】:2021-12-16 08:51:08
【问题描述】:

我需要在对话框片段上显示一些自定义按钮,但警报对话框只有 .setPositiveButton AND .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()
        }
    }

但没有一个按我的意愿工作

【问题讨论】:

    标签: android kotlin android-alertdialog


    【解决方案1】:

    如果我理解正确,您需要创建一个自定义布局并将其提供给您的 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()
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-24
      • 1970-01-01
      • 1970-01-01
      • 2012-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多