Kotlin - custom dialog in Android

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

    Below my solution as a kind of "message box". I have not implemented an "OK" button. The message box should close after clicking on it.

    Here the layout element (*/layout/message_box.xml)

    
    
    
        
    
            
    
            
    
        
    
    

    This function I've implement in a Fragment class. It is written in Kotlin.

    fun showMessageBox(text: String){
    
        //Inflate the dialog as custom view
        val messageBoxView = LayoutInflater.from(activity).inflate(R.layout.message_box, null)
    
        //AlertDialogBuilder
        val messageBoxBuilder = AlertDialog.Builder(activity).setView(messageBoxView)
    
        //setting text values
        messageBoxView.message_box_header.text = "This is message header"
        messageBoxView.message_box_content.text = "This is message content"
    
        //show dialog
        val  messageBoxInstance = messageBoxBuilder.show()
    
        //set Listener
        messageBoxView.setOnClickListener(){
            //close dialog
            messageBoxInstance.dismiss()
        }
    }
    

提交回复
热议问题