在两个警报对话框之间传递信息

时间:2019-04-03 11:00:20

标签: android kotlin alertdialog

第一个警报对话框中的ImageView将打开第二个对话框,以在第一个对话框中更改ImageView的imageResource。但是我不知道如何在两个警报对话框之间建立连接

两者的xml布局都不同,所以我假设在第二个对话框中,我应该引用第一个对话框的布局

private fun editItemDialog() {
    val dialogBuilder1 = AlertDialog.Builder(this)
    val inflater = this.layoutInflater
    val dialogView = inflater.inflate(R.layout.edit_dialog, null)
    dialogBuilder1.setView(dialogView)

    var editIconButton = dialogView.findViewById<View>(R.id.editIcon) as ImageView

    editIconButton.setOnClickListener{
        showIconDialog()

    }



    dialogBuilder1.setTitle("Edit mode")
    dialogBuilder1.setPositiveButton("Save") { _, _ ->
       //sth
    }
    dialogBuilder1.setNegativeButton("Cancel") { _, _ ->
       //sth
    }
    val b = dialogBuilder1.create()
    b.show()
}     

private fun showIconDialog() {
    val dialogBuilder = AlertDialog.Builder(this)
    val inflater = this.layoutInflater
    val dialogView = inflater.inflate(R.layout.icons, null)
    dialogBuilder.setView(dialogView)

    //examplary two icons to select

    var travelRB = dialogView.findViewById<View>(R.id.travel) as RadioButton

    var travRB = dialogView.findViewById<View>(R.id.travel) as RadioButton


    dialogBuilder.setTitle("Icon dialog")
    dialogBuilder.setMessage("Select an icon")
    dialogBuilder.setPositiveButton("Save") { _, _ ->
         //here I would like to change an icon of the ImageView, for example:
         editIconButton.setImageResource(R.id.travel)

    dialogBuilder.setNegativeButton("Cancel") { _, _ ->
        //sth
    }
    val b = dialogBuilder.create()
    b.show()


}

1 个答案:

答案 0 :(得分:4)

您可以在第二个对话框中添加回调

fun showIconDialog(callback : (Drawable) -> Unit) { 
        //code 
        callback.invoke(someDrawable)
    }

在第一个上,您只需执行以下操作:

showIconDialog() { someDrawable ->
        //code to change the layout src icon
    }