美文网首页Android Kotlin
Koltin 官方Android开源库 ANKO 详解 一

Koltin 官方Android开源库 ANKO 详解 一

作者: Kael_Zhang的安卓笔记 | 来源:发表于2022-08-22 10:14 被阅读0次

    引言

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

    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

    常规代码如下:

    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"
    )
    
    • Make a call
    makeCall(number) // without tel
    
    • send sms
    sendSMS(number, [text]) //without sms
    
    • Browse
    browse(url)
    

    相关文章

      网友评论

        本文标题:Koltin 官方Android开源库 ANKO 详解 一

        本文链接:https://www.haomeiwen.com/subject/wmbzmrtx.html