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