Kotlin/Anko按钮onClick无法正常工作 [英] Kotlin / Anko button onClick not working
问题描述
我对Kotlin还是很陌生,并且我正在使用Anko DSL(带有一些XML)来生成警报.我的问题是,当我单击按钮时,onClick{ ... }
函数不会发生.其他一切都正常,只是这个问题
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()
}
推荐答案
positiveButton
接受的lambda参数不是设置函数,而是单击侦听器本身,因此您可以在其中直接编写代码:
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")
}
您在其中调用的onClick
函数来自另一个外部作用域,并且覆盖了外部视图之一的侦听器,可能是XML中所包含视图根的侦听器.
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.
这篇关于Kotlin/Anko按钮onClick无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!