Skip to content

Longan – Dialogs

DylanCai edited this page Aug 16, 2021 · 1 revision

保留了 Anko 的用法并做了优化。

Toasts

简单地显示 Toast 消息,修复了 Android 7.x 的 BadTokenException 异常。

toast("Hi there!")
toast(R.string.message)
longToast("Wow, such duration")

SnackBars

简单地显示 SnackBar 消息。

snackbar("Hi there!")
snackbar(R.string.message)
longSnackbar("Wow, such duration")
indefiniteSnackbar("Wow, always show")
snackbar("Action, reaction", "Click me!") { doStuff() }

Alerts

用 DSL 语法显示对话框.

alert("Hi, I'm Roy", "Have you tried turning it off and on again?") {
  okButton { toast("Oh…") }
  cancelButton {}
}

上面的代码默认显示 MaterialAlertDialog,如果你想切换成默认使用 Appcompat 的AlertDialog,可以配置 AlertFactory

initAlertBuilderFactory(Appcompat)

或者切换某一次对话框的样式:

alert(Appcompat, "Some text message")

默认提供 AppcompatMaterial 可选,你可以实现 AlertBuilderFactory 接口进行自定义。后续会对更多的弹框样式进行支持。

Selectors

selector() 显示一个带有文本项列表的对话框:

val countries = listOf("China", "Russia", "USA", "Australia")
selector(countries, "Where are you from?") { dialog, i ->
  toast("So you're living in ${countries[i]}, right?")
}

singleChoiceSelector() 显示一个单选列表的对话框:

private var checkedCountry = "China"

private fun selectCountry() {
  val countries = listOf("China", "Russia", "USA", "Australia")
  val checkedIndex = countries.indexOfFirst { it == checkedCountry }
  singleChoiceSelector(countries, checkedIndex, "Where are you from?") { dialog, i ->
    checkedCountry = countries[i]
    toast("You're living in ${checkedCountry}.")
    dialog.dismiss()
  }
}

multiChoiceSelector() 显示一个多选列表的对话框:

private val foods = listOf("Apple", "Banana", "Pear", "Peach")
private val checkedItems = BooleanArray(foods.size)

private fun selectFoods() {
  multiChoiceSelector(foods, checkedItems, "What do you want to eat?") { dialog, i, isChecked ->
    checkedItems[i] = isChecked
  }.doOnDismiss {
    toast("So you want to eat ${checkedItems.filter { it }.size} foods.")
  }
}
Clone this wiki locally