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

标签 android kotlin android-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()


}

最佳答案

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

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

在第一个上你只需要这样做:

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

关于android - 在两个警报对话框之间传递信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55493568/

相关文章:

javascript - Cloud Functions for Firebase - 每天在特定时间更新值

android - 原始 kapt 已弃用

kotlin - 上下循环

android - 添加 AndroidInjector 时 Dagger 缺少绑定(bind)

Android:当用户第二次打开对话框时,对话框 fragment 并保留第一次打开对话框时选择的值

java - MaterialDialog 表单不起作用

android - 如何在执行过程中停止 OnClickListener?

android - RecyclerView 无限无限滚动问题

java - 升级到 java 8 后无法使用 crashlytics 发布项目

java - Android 使用 reSTLet 绕过配置文件代理