AndroidのUI構築用DSLライブラリの「Anko」でアラートダイアログ(Alert Dialog)を簡単に作れるので紹介です。
環境
- kotlinバージョン: 1.1.3-2
- ankoバージョン: 0.10.1
準備
gradleに以下を追記
dependencies {
compile "org.jetbrains.anko:anko:$anko_version"
}
標準のダイアログ
alert("Message") {
title = "Title"
yesButton {}
noButton {}
}.show()
ボタンをカスタマイズしたダイアログ
alert("Message") {
title = "Title"
positiveButton("Positive") {}
negativeButton("Negative") {}
neutralPressed("Neutral", {toast("Foo")})
}.show()
↑みたいにトーストも簡単にできる。
CustomViewを含んだダイアログ
alert("Message") {
title = "Title"
yesButton {}
noButton {}
customView {
linearLayout {
padding = dip(16)
textView("Input: ")
editText {
hint = "Hint text"
}.lparams(width = matchParent)
}
}
}.show()
参考
Anko Commons – Dialogs · Kotlin/anko Wiki
https://github.com/Kotlin/anko/wiki/Anko-Commons-%E2%80%93-Dialogs