问题
I'm fairly new to Kotlin, and I'm using Anko DSL (with some XML) to generate an alert. My issue is, the onClick{ ... }
function doesn't happen when I click the button. Everything else works fine, it's just this one issue
fab.setOnClickListener { view ->
alert {
title = "Add Board"
customView {
include<View>(R.layout.alert_xml) {
this.spinner.adapter = adapter
info("Alert loaded")
val boardSpinner = this.spinner
val boardText = this.board_text
positiveButton("OK") {
onClick {
info("Testing")
}
}
}
}
}.show()
}
回答1:
The lambda parameter that positiveButton
takes is not a setup function, but the click listener itself, so you can write your code directly inside it:
positiveButton("OK") {
info("Testing")
}
The onClick
function that you're calling inside it is coming from another outer scope, and is overriding the listener of one of the outer views, presumably the listener for the root of the included view from the XML.
来源:https://stackoverflow.com/questions/44532833/kotlin-anko-button-onclick-not-working