android - 在两个警报对话框之间传递信息
问题描述
第一个警报对话框中的 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
}
推荐阅读
- powershell - powershell破坏脚本中的特殊字符?
- azure - 从 Azure Data Lake Storage 复制文件
- blazor - Blazor 应用加载 boostrap-dark.css 文件两次
- php - PHP PDO 查询,优化超过 1000 万行的速度性能 MS ACCESS 数据库
- javascript - 为什么我的 innerHTML 不返回数组对象?
- checkbox - 从树视图 UWP 中获取选定的节点
- c# - 为什么在 c# 中启用计时器时不能做任何其他事情
- excel - ID,按 ID 计算 Y
- javascript - 当页面的提交按钮被点击时,组件是如何知道的?
- postgresql - 气流将 postgres 数据库的所有表导出到 BigQuery