Kotlin / Anko button onClick not working

风流意气都作罢 提交于 2019-12-11 05:46:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!