android布局DSL,android – 使用自定义Anko布局DSL解除警报对话框
我創建了以下警告對話框,其中包含一個包含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()
我想在單擊按鈕并執行if-else子句時忽略它.我嘗試使用這個@ alert但它沒有提供對話框方法.
解決方法:
這是有問題的,因為當對話框尚不存在時,您的按鈕函數調用會注冊偵聽器.
這是一種方法,使用本地lateinit變量使對話框在偵聽器中可用:
lateinit var dialog: DialogInterface
dialog = alert {
customView {
button("Click") {
dialog.dismiss()
}
}
}.show()
您還可以將構建器的結果分配給類屬性等.請注意,局部變量的lateinit可用于since Kotlin 1.2.
標簽:anko,android,kotlin
來源: https://codeday.me/bug/20190727/1550077.html
總結
以上是生活随笔為你收集整理的android布局DSL,android – 使用自定义Anko布局DSL解除警报对话框的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android sharedprefer
- 下一篇: android元素离边框间距,Recyc