Kotlin - Android 中的自定义对话框

新手上路,请多包涵

我想在 Kotlin 中创建一个 custom dialog 。我在 Stack Overflow 上查看了有关此主题的问题,但找不到任何有用的信息。我该怎么做?

原文由 Abduqodir Ubaydullayev 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 828
2 个回答

您可以将以下代码用于自定义对话框。这是我的工作代码。

  private fun showDialog(title: String) {
    val dialog = Dialog(activity)
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.setCancelable(false)
    dialog.setContentView(R.layout.custom_layout)
    val body = dialog.findViewById(R.id.body) as TextView
    body.text = title
    val yesBtn = dialog.findViewById(R.id.yesBtn) as Button
    val noBtn = dialog.findViewById(R.id.noBtn) as TextView
    yesBtn.setOnClickListener {
        dialog.dismiss()
    }
    noBtn.setOnClickListener { dialog.dismiss() }
    dialog.show()

}

原文由 Shohel Rana 发布,翻译遵循 CC BY-SA 4.0 许可协议

我创建了这个 Kotlin 类:

 class AnimDialog(private val context: Context) {
    private val dialog = Dialog(context)
    fun test(s: String) {
        Log.i("TAG", "test: $s")
    }
init{

    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
    dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE)
    dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
}
    fun one(headerImage :Int?=null,
        description: String,
            titleOfPositiveButton: String? = null,
            titleOfNegativeButton: String? = null,
            positiveButtonFunction: (() -> Unit)? = null,
            negativeButtonFunction: (() -> Unit)? = null
    ) :Dialog{

        dialog.setCancelable(false)
        dialog.setContentView(R.layout.dialog_one)
        val dialogHeaderImage=dialog.findViewById(R.id.imageHead_dia) as ImageView
        val dialogDescription = dialog.findViewById(R.id.description) as TextView
        val dialogPositiveButton = dialog.findViewById(R.id.positiveButton) as Button
        val dialogNegativeButton = dialog.findViewById(R.id.negativeButton) as Button

        headerImage?.let { dialogHeaderImage.setImageResource(it) }
        dialogHeaderImage.
        startAnimation(
            AnimationUtils.loadAnimation(
                context,
                R.anim.bounce
            )
        )
        dialogDescription.text = description
        titleOfPositiveButton?.let { dialogPositiveButton.text = it } ?: dialogPositiveButton.text
        titleOfNegativeButton?.let { dialogNegativeButton.text = it } ?: dialogNegativeButton.text
        dialogPositiveButton.setOnClickListener {
            positiveButtonFunction?.invoke()
            dialog.dismiss()
        }
        dialogNegativeButton.setOnClickListener {
            negativeButtonFunction?.invoke()
            dialog.dismiss()
        }

return dialog

    }

您必须在哪里定义:

1-布局 <dialog_one> 包括正面btn,负面btn,图像视图,文本视图

2- 动画 <bounce> 如果您想将动画添加到标题图像

现在在 活动 中:

  val animDialog = AnimDialog(this)

        animDialog.one(
            headerImage = android.R.drawable.ic_delete,
            description = "Sometimes we looking for new dialog to fill better \n ",
            titleOfPositiveButton = "Accept it",
            titleOfNegativeButton = "Not now",
            positiveButtonFunction = {
                animDialog.test("OK")
            },
            negativeButtonFunction = {
                animDialog.test("NO")
            }).show()

在此处输入图像描述

原文由 Mori 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题