Ale*_*mbo 3 android kotlin anko
我创建了以下警告对话框,其中包含一个简单的视图TextView,EditText并且Button:
alert {
customView {
verticalLayout {
textView {
text = getString(R.string.enter_quantity)
textSize = 18f
textColor = Color.BLACK
}.lparams {
topMargin = dip(17)
horizontalMargin = dip(17)
bottomMargin = dip(10)
}
val quantity = editText {
inputType = InputType.TYPE_CLASS_NUMBER
background = ContextCompat.getDrawable(this@ProductsList, R.drawable.textbox_bg)
}.lparams(width = matchParent, height = wrapContent) {
bottomMargin = dip(10)
horizontalMargin = dip(17)
}
button(getString(R.string.confirm)) {
background = ContextCompat.getDrawable(this@ProductsList, R.color.colorPrimary)
textColor = Color.WHITE
}.lparams(width = matchParent, height = matchParent) {
topMargin = dip(10)
}.setOnClickListener {
if (quantity.text.isNullOrBlank())
snackbar(parentLayout!!, getString(R.string.enter_valid_quantity))
else
addToCart(product, quantity.text.toString().toInt())
}
}
}
}.show()
Run Code Online (Sandbox Code Playgroud)
我想在点击按钮并if-else执行该子句时忽略它.我尝试过使用this@alert但它没有提供对话框方法.
这是有问题的,因为button当对话框尚不存在时,您的函数调用会注册侦听器.
这是一种方法,使用局部lateinit变量使dialog侦听器内部可用:
lateinit var dialog: DialogInterface
dialog = alert {
customView {
button("Click") {
dialog.dismiss()
}
}
}.show()
Run Code Online (Sandbox Code Playgroud)
您还可以将构建器的结果分配给类属性等.请注意,自Kotlin 1.2起,lateinit可以使用局部变量.
| 归档时间: |
|
| 查看次数: |
1497 次 |
| 最近记录: |