Android Kotlin

Koltin 官方Android开源库 ANKO 详解 一

2022-08-22  本文已影响0人  Kael_Zhang的安卓笔记

引言

anko 是kotlin官方(JetBrains)针对android快速开发 而开源的一个库,主要的目的是以代码取代xml来书写UI布局、包含了很多常用的函数和功能 以减少大量的模板代码(类似 android官方的 各种 ktx 库),本文主要介绍后者

Anko Commons – Dialogs

dependencies {
    implementation "org.jetbrains.anko:anko-commons:$anko_version"
    implementation "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
}
toast("Hi there!")
toast(R.string.message)
longToast("Wow, such duration")
view.snackbar("Hi there!")
view.snackbar(R.string.message)
view.longSnackbar("Wow, such duration")
view.snackbar("Action, reaction", "Click me!") { doStuff() }
alert("Hi, I'm Roy", "Have you tried turning it off and on again?") {
    yesButton { toast("Oh…") }
    noButton {}
}.show()

alert(Appcompat, "Some text message").show()
val countries = listOf("a", "b", "c", "d")
selector("Where are you from?", countries, { dialogInterface, i ->
    toast("So you're living in ${countries[i]}, right?")
})
val dialog = progressDialog(message = "Please wait a bit…", title = "Fetching data")

Anko Coroutines(线程切换)

dependencies {
    implementation "org.jetbrains.anko:anko-coroutines:$anko_version"
}

Anko Commons – Intents

dependencies {
    implementation "org.jetbrains.anko:anko-commons:$anko_version"
}

常规代码如下:

val intent = Intent(this, SomeOtherActivity::class.java)
intent.putExtra("id", 5)
intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)

anko代码

//等价于上面的常规代码
startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
//传递单数据
startActivity<SomeOtherActivity>("id" to 5)
//传递多数据
startActivity<SomeOtherActivity>(
    "id" to 5,
    "city" to "Denpasar"
)
makeCall(number) // without tel
sendSMS(number, [text]) //without sms
browse(url)
上一篇 下一篇

猜你喜欢

热点阅读