Kotlin - custom dialog in Android

后端 未结 6 1730
醉话见心
醉话见心 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:08

    This is the way by which you can create your own dialog with custom layout.

    val dialogBuilder = AlertDialog.Builder(context, R.style.AlertDialogTheme)
        val inflater = this.layoutInflater
        val dialogView = inflater.inflate(R.layout.layout_chat_type_selection, null)
        dialogBuilder.setView(dialogView)
        val radioGroupChat = dialogView.radio_group_chat
        dialogView.radioButton_user_chat.isChecked = true
        dialogBuilder.setPositiveButton(getString(R.string.ok_text), object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface, id: Int) {
                when (radioGroupChat.checkedRadioButtonId) {
                    R.id.radioButton_user_chat -> {
                        (activity as HomeActivity).replaceFragment(MySkippersFragment.getInstance(isFromChat = true))
                    }
                    R.id.radioButton_circle_chat -> {
                        (activity as HomeActivity).replaceFragment(PickCircleFragment.getInstance(
                            PickCircleFragment.NEW_CIRCLE_CHAT), true)
                    }
                }
            }
        })
        dialogBuilder.setNegativeButton(getString(R.string.cancel_text), object : DialogInterface.OnClickListener {
            override fun onClick(dialog: DialogInterface?, which: Int) {
            }
        })
    
        val alertDialog = dialogBuilder.create()
        alertDialog.show()
    

提交回复
热议问题