Kotlin - custom dialog in Android

后端 未结 6 1732
醉话见心
醉话见心 2020-12-30 20:00

I want to create a custom dialog in Kotlin. I looked through questions on this theme on Stack Overflow, but I could not find any useful informa

6条回答
  •  梦谈多话
    2020-12-30 20:14

    If someone wants to show 2 buttons on dialog ( GENERIC SOLUTION ) KOTLIN

       fun createCustomTwoButtonDialog(
        msg: String, context: Context?
        , positiveButtonText: String, negativeButtonText: String,
        isCancellable: Boolean,
        dialogListener: DialogListener
    ) {
        context?.let { context ->
            val builder =
                AlertDialog.Builder(context)
            builder.setTitle("Your desired title")
            builder.setMessage(msg)
            builder.setCancelable(isCancellable)
            builder.setPositiveButton(positiveButtonText) { dialogInterface: DialogInterface?, i: Int ->
                dialogListener.onPositiveClick()
                dialogInterface?.dismiss()
            }
            builder.setNegativeButton(negativeButtonText)
            { dialogInterface: DialogInterface?, i: Int ->
                dialogListener.onNegativeClick()
                dialogInterface?.dismiss()
            }
            val alertDialog = builder.create()
            alertDialog.show()
        }
    }
    

    DialogListener is interface with 2 methods onNegativeClick() and onPositiveClick()

提交回复
热议问题