如何在Anko DSL中引用其他视图?

时间:2015-12-11 06:58:11

标签: android kotlin anko

我在Android项目中使用Anko,但我不知道当引用的视图与我引用的视图不同时,它如何引用我在DSL中创建的子视图。

以下代码有效:

alert {
    customView {
        val input = textInputLayout {
            editText {
                hint = "Name"
                textColor =resources.getColor(R.color.highlight)
            }
        }


        positiveButton("OK") { "${input.editText.text}" }
    }
}.show()

但以下代码不起作用:

alert {
    customView {
        val vertical = verticalLayout {
            textView {
                text = "Edit device name"
                textColor = resources.getColor(R.color.highlight)
                textSize = 24F
            }
            val input = textInputLayout {
                editText {
                    hint = "Name"
                    textColor = resources.getColor(R.color.highlight)
                }
            }
        }

        positiveButton("OK") { "${vertical.input.editText.text}" }  // Cannot resolve "input"
    }
}.show()

5 个答案:

答案 0 :(得分:6)

我认为有两种方法。超级hacky方式是在textInputLayout块内声明正面按钮。这是可能的,因为您可以从任何嵌套范围内访问所有外部范围,并在positiveButton范围内声明alert方法:

alert {
    customView {
        verticalLayout {
            textInputLayout {
                val editText = editText {
                    hint = "Name"
                }

                positiveButton("OK") { toast("${editText.text}") }
            }
        }
    }
}.show()

声明一个可以从两个范围访问的变量的方式就越少。但是,您需要将其设置为可为空,因为您无法立即对其进行初始化:

alert {
    var editText: EditText? = null

    customView {
        verticalLayout {
            textInputLayout {
                editText = editText {
                    hint = "Name"
                }
            }
        }
    }

    positiveButton("OK") { toast("${editText!!.text}") } 
}.show()

答案 1 :(得分:2)

我建议使用findViewById()

alert {
        customView {
            val vertical = verticalLayout {
                textView {
                    text = "Edit device name"
                    textSize = 24F
                }
                val input = textInputLayout {
                    editText {
                        id = R.id.my_id_resource // put your id here
                        hint = "Name"
                    }
                }
            }
            positiveButton("OK") { "${(vertical.findViewById(R.id.my_id_resource) as? EditText)?.text}" }  
        }
    }.show()

答案 2 :(得分:1)

您始终可以提升视图,手动传递上下文vertical

customView {
    val vertical = verticalLayout {
        textView {
            text = "Edit device name"
            textColor = resources.getColor(R.color.highlight)
            textSize = 24F
        }
    }

    val input = /*here:*/ vertical.textInputLayout {
        editText {
            hint = "Name"
            textColor = resources.getColor(R.color.highlight)
        }
    }

    positiveButton("OK") { "${input.editText.text}" }
}

答案 3 :(得分:1)

我通常将视图声明为具有lateinit修饰符的类中的属性;这样它就不可为空,大多数视图都在一个地方声明,提高了可读性:

lateinit var toolbar: Toolbar

...
appBarLayout {
    toolbar = toolbar {}.lparams(width = matchParent, height = matchParent)
             }.lparams(width = matchParent)
...
setSupportActionBar(toolbar)

答案 4 :(得分:0)

可能最好的方法是为以后需要引用的元素和find<T : View>(Int) : T函数使用Android ID。这允许您从任何地方引用它们,只要视图仍然存在,并且您可以访问应用程序/活动范围。

有关详细信息,请参阅LocationRequest

示例案例:动态添加按钮到现有视图

verticalLayout {
  id = R.id.button_container
}
//note that the code below here may be executed anywhere after the above in your onCreate function
//verticalLayout is a Anko subclass of LinearLayout, so using the android class is valid.
val buttonContainer = find<LinearLayout>(R.id.button_container)

val containerContext = AnkoContext.Companion.create(ctx, buttonContainer)
val button = ctx.button {
  text = "click me"
  onClick = { toast("created with IDs!") }
}
buttonContainer.addView(button.createView(containerContext, buttonContainer))