如何在Alertdialog中添加听众论点的解雇

发布于 2025-02-13 15:41:35 字数 3123 浏览 0 评论 0原文

我正在采用一种创建自定义AlertDialog的方法,可以在其中调用此方法传递的文本,并在任何地方打开对话框。

我的问题是,当我使用它时,我需要在自定义视图的每个按钮中拨出一个解散,但是当我调用此方法时,系统无需了解对话框的任何信息。

因此,我需要对通过的听众或类似的东西添加“解雇”。

private fun showCustomAlertDialog(context: Context, title: String?, message: String?, positiveButtonClickListener: View.OnClickListener?, negativeButtonClickListener: View.OnClickListener?, neutralButtonClickListener: View.OnClickListener?, positiveText: String?, negativeText: String?, neutralText: String?){

        val layoutInflater: LayoutInflater = layoutInflater
        val alertLayout: View = layoutInflater.inflate(R.layout.layout_dialog, null)
        val tvDialogTitle: TextView = alertLayout.findViewById(R.id.tv_dialog_title)
        val tvDialogDescription: TextView = alertLayout.findViewById(R.id.tv_dialog_description)
        val btnAccept: Button = alertLayout.findViewById(R.id.btn_dialog_accept)
        val btnCancel: Button = alertLayout.findViewById(R.id.btn_dialog_cancel)
        val btnNeutral: Button = alertLayout.findViewById(R.id.btn_dialog_neutral)

        val alertDialog = AlertDialog.Builder(context)

        alertDialog.setCancelable(false)
        alertDialog.setView(alertLayout)
        val dialog = alertDialog.create()

        if (title != null) {
            if(title.isNotEmpty()){
                tvDialogTitle.text = title
            }
        }else{
            tvDialogTitle.text = ""
        }

        if (message != null) {
            if(message.isNotEmpty()){
                tvDialogDescription.text = message
            }
        }else{
            tvDialogDescription.text = ""
        }

        if(neutralButtonClickListener != null && (positiveButtonClickListener == null && negativeButtonClickListener == null)){
            btnAccept.visibility = View.GONE
            btnCancel.visibility = View.GONE
            btnNeutral.visibility = View.VISIBLE

            btnNeutral.setOnClickListener(neutralButtonClickListener)
            if (neutralText != null) {
                if(neutralText.isNotEmpty()){
                    btnNeutral.text = neutralText
                }
            }
        }

        if(neutralButtonClickListener == null && (positiveButtonClickListener != null && negativeButtonClickListener != null)) {
            btnAccept.visibility = View.VISIBLE
            btnCancel.visibility = View.VISIBLE
            btnNeutral.visibility = View.GONE

            btnAccept.setOnClickListener(positiveButtonClickListener)
            btnCancel.setOnClickListener(negativeButtonClickListener)

            if (positiveText != null) {
                if(positiveText.isNotEmpty()){
                    btnAccept.text = positiveText
                }
            }

            if (negativeText != null) {
                if(negativeText.isNotEmpty()){
                    btnCancel.text = negativeText
                }
            }

        }

        dialog.show()

    }

目前,我不知道如何在进行我的听众之后如何添加解雇。例如:

btnAccept.setOnClickListener(positiveButtonClickListener)

有建议吗?

谢谢

I'm doing a method to create a custom AlertDialog in which I can call this method passing text, and listener to open a dialog in everywhere.

My problem is that when I'm use it, I need to call a dismiss in every button of the custom view, but when I call this method the system no need to know anything about the dialog.

So, I need to add a "dismiss" on the passed listener or something like that.

private fun showCustomAlertDialog(context: Context, title: String?, message: String?, positiveButtonClickListener: View.OnClickListener?, negativeButtonClickListener: View.OnClickListener?, neutralButtonClickListener: View.OnClickListener?, positiveText: String?, negativeText: String?, neutralText: String?){

        val layoutInflater: LayoutInflater = layoutInflater
        val alertLayout: View = layoutInflater.inflate(R.layout.layout_dialog, null)
        val tvDialogTitle: TextView = alertLayout.findViewById(R.id.tv_dialog_title)
        val tvDialogDescription: TextView = alertLayout.findViewById(R.id.tv_dialog_description)
        val btnAccept: Button = alertLayout.findViewById(R.id.btn_dialog_accept)
        val btnCancel: Button = alertLayout.findViewById(R.id.btn_dialog_cancel)
        val btnNeutral: Button = alertLayout.findViewById(R.id.btn_dialog_neutral)

        val alertDialog = AlertDialog.Builder(context)

        alertDialog.setCancelable(false)
        alertDialog.setView(alertLayout)
        val dialog = alertDialog.create()

        if (title != null) {
            if(title.isNotEmpty()){
                tvDialogTitle.text = title
            }
        }else{
            tvDialogTitle.text = ""
        }

        if (message != null) {
            if(message.isNotEmpty()){
                tvDialogDescription.text = message
            }
        }else{
            tvDialogDescription.text = ""
        }

        if(neutralButtonClickListener != null && (positiveButtonClickListener == null && negativeButtonClickListener == null)){
            btnAccept.visibility = View.GONE
            btnCancel.visibility = View.GONE
            btnNeutral.visibility = View.VISIBLE

            btnNeutral.setOnClickListener(neutralButtonClickListener)
            if (neutralText != null) {
                if(neutralText.isNotEmpty()){
                    btnNeutral.text = neutralText
                }
            }
        }

        if(neutralButtonClickListener == null && (positiveButtonClickListener != null && negativeButtonClickListener != null)) {
            btnAccept.visibility = View.VISIBLE
            btnCancel.visibility = View.VISIBLE
            btnNeutral.visibility = View.GONE

            btnAccept.setOnClickListener(positiveButtonClickListener)
            btnCancel.setOnClickListener(negativeButtonClickListener)

            if (positiveText != null) {
                if(positiveText.isNotEmpty()){
                    btnAccept.text = positiveText
                }
            }

            if (negativeText != null) {
                if(negativeText.isNotEmpty()){
                    btnCancel.text = negativeText
                }
            }

        }

        dialog.show()

    }

At this moment I don't know how to add a dismiss after doing the listener that I passed. For example:

btnAccept.setOnClickListener(positiveButtonClickListener)

Any advice?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

公布 2025-02-20 15:41:36
fun showCustomDialog(
            context: Context,
            title: String?,
            onPositiveButtonClicked: (() -> Unit)? = null,
            onNegativeButtonClicked: (() -> Unit)? = null
        ) {
            val binding =
                LayoutCustomDialogBinding.inflate(LayoutInflater.from(context))

            val alertDialog = MaterialAlertDialogBuilder(context)
                .setView(binding.root)
                .create()

            binding.positiveButton.setOnClickListener {
                 alertDialog.dismiss()
                 onPositiveButtonClicked?.invoke()
            }

            binding.negativeButton.setOnClickListener {
                 alertDialog.dismiss()
                 onNegativeButtonClicked?.invoke()
            }
            
            alertDialog.show()
         }

用法:

showCustomDialog(
    context = context,
    title = "Title",
    onPositiveButtonClicked = {
        //do your action
    }, 
    onNegativeButtonClicked = {
        //do your action
    }
)
fun showCustomDialog(
            context: Context,
            title: String?,
            onPositiveButtonClicked: (() -> Unit)? = null,
            onNegativeButtonClicked: (() -> Unit)? = null
        ) {
            val binding =
                LayoutCustomDialogBinding.inflate(LayoutInflater.from(context))

            val alertDialog = MaterialAlertDialogBuilder(context)
                .setView(binding.root)
                .create()

            binding.positiveButton.setOnClickListener {
                 alertDialog.dismiss()
                 onPositiveButtonClicked?.invoke()
            }

            binding.negativeButton.setOnClickListener {
                 alertDialog.dismiss()
                 onNegativeButtonClicked?.invoke()
            }
            
            alertDialog.show()
         }

Usage:

showCustomDialog(
    context = context,
    title = "Title",
    onPositiveButtonClicked = {
        //do your action
    }, 
    onNegativeButtonClicked = {
        //do your action
    }
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文