Koltin 官方Android開(kāi)源庫(kù) ANKO 詳解 一

引言

anko 是kotlin官方(JetBrains)針對(duì)android快速開(kāi)發(fā) 而開(kāi)源的一個(gè)庫(kù),主要的目的是以代碼取代xml來(lái)書(shū)寫(xiě)UI布局、包含了很多常用的函數(shù)和功能 以減少大量的模板代碼(類似 android官方的 各種 ktx 庫(kù)),本文主要介紹后者

Anko Commons – Dialogs

  • implementation
dependencies {
    implementation "org.jetbrains.anko:anko-commons:$anko_version"
    implementation "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
}
  • toast
toast("Hi there!")
toast(R.string.message)
longToast("Wow, such duration")
  • SnackBars
view.snackbar("Hi there!")
view.snackbar(R.string.message)
view.longSnackbar("Wow, such duration")
view.snackbar("Action, reaction", "Click me!") { doStuff() }
  • Alerts
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()
  • Selectors
val countries = listOf("a", "b", "c", "d")
selector("Where are you from?", countries, { dialogInterface, i ->
    toast("So you're living in ${countries[i]}, right?")
})
  • Progress dialogs
val dialog = progressDialog(message = "Please wait a bit…", title = "Fetching data")

Anko Coroutines(線程切換)

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

Anko Commons – Intents

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

常規(guī)代碼如下:

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

anko代碼

//等價(jià)于上面的常規(guī)代碼
startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
//傳遞單數(shù)據(jù)
startActivity<SomeOtherActivity>("id" to 5)
//傳遞多數(shù)據(jù)
startActivity<SomeOtherActivity>(
    "id" to 5,
    "city" to "Denpasar"
)
  • Make a call
makeCall(number) // without tel
  • send sms
sendSMS(number, [text]) //without sms
  • Browse
browse(url)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容