Anko之Anko Commons(二)

承接上一部分的anko layout,元旦過后,給大家來一波知識,恢復(fù)一下coding...;上一部分地址anko_layout.

文本來源
英文鏈接 Anko Commons is a "toolbox" for Kotlin Android developer. The library contains a lot of helpers for Android SDK, including, but not limited to:

   1. Intents (wiki);
   2. Dialogs and toasts (wiki);
   3. Logging (wiki);
   4. Resources and dimensions (wiki).

anko Commons作為Android Kotlin發(fā)展的一個工具盒,這個依賴包在Android SDK中包含了許多好處

Intents(用于頁面跳轉(zhuǎn)的意圖)

1. 加入需要的依賴
 dependencies {
            compile "org.jetbrains.anko:anko-commons:版本號"
        }
2.  以前原生Kotlin的寫法
 val intent = Intent(this, SomeOtherActivity::class.java)
    intent.putExtra("id", 5)
    intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP)
    startActivity(intent)
3. 現(xiàn)在的簡寫
   startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
   如果參數(shù)少,還可以更簡單的寫:
     startActivity<SomeOtherActivity>("id" to 5)
4. Useful Intent callers
    Anko has call wrappers for some widely used Intents:
    Goal              Solution
    Make a call         makeCall(number) without tel:
    Send a text         sendSMS(number, [text]) without sms:
    Browse the web  browse(url)
    Share some text share(text, [subject])
    Send a email    email(email, [subject], [text])
    Arguments in square brackets ([]) are optional. Methods return true if the intent was send.

Dialogs and toasts

>1. 第一步同樣導(dǎo)入依賴
  dependencies {
        compile "org.jetbrains.anko:anko-commons:$anko_version"
         compile "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
}
2. Toast簡寫,更加方便簡潔
    toast("直接填寫需要的內(nèi)容")//默認(rèn)是短按彈出Toast
    toast(R.string.message)//也可以使用資源文件中的文本內(nèi)容
    longToast("長按彈出提示內(nèi)容")
3. SnackBars的使用(android 5.0還是6.0的一個新的屬性,類似Toast效果,但是更加強(qiáng)大的顯示)必須引入材料設(shè)計的依賴:
     compile 'com.android.support:design:sdk版本號'
    //view 代表是在哪個控件之下顯示。
    snackbar(view, "Hi there!")
    snackbar(view, R.string.message)
    longSnackbar(view, "Wow, such duration")
    snackbar(view, "Action, reaction", "Click me!") { doStuff() }
4. A small DSL for showing alert dialogs(對話框)
    alert ("標(biāo)題(title))", "內(nèi)容(content)"){
             yesButton {}//確定按鈕
             noButton {}//取消按鈕
         }.show()//系統(tǒng)默認(rèn)的
5. 自定義的對話框,包括自定義title
     alert {
             customView {
                 editText()
             }
             customTitle { 
             }
         }.show()
6. Progress dialog(進(jìn)度條的對話框)
    val dialogPress = ProgressDialog(activity)
7. Selectors(多條目的點(diǎn)擊事件彈出框)
    val countries = listOf("Russia", "USA", "Japan", "Australia")
        selector("Where are you from?", countries, { dialogInterface, i ->
             toast("So you're living in ${countries[i]}, right?")
    })

Logging

日志打印anko使用的是 AnkoLogger

 必須要繼承 AnkoLogger
    info("London is the capital of Great Britain")
    debug(5) // .toString() method will be executed
    warn(null) // "null" will be printed
 也可以這樣寫
     private val log = AnkoLogger<當(dāng)前Activity>(this)
     private val logWithASpecificTag = AnkoLogger("my_tag")//設(shè)置特別的標(biāo)志
     private fun someMethod() {
        log.warning("Big brother is watching you!")
     }

Resources and dimensions

資源文件

    對于此部分到此結(jié)束?。?!
   《Anko之Anko SQLite(三)》敬請期待...
    歡迎評閱!
    郵箱:simoncqhy@163.com
    如果有贊賞當(dāng)然就更好了,是給我的肯定和前進(jìn)的動力。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

  • afinalAfinal是一個android的ioc,orm框架 https://github.com/yangf...
    passiontim閱讀 15,873評論 2 45
  • Correctness AdapterViewChildren Summary: AdapterViews can...
    MarcusMa閱讀 9,053評論 0 6
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,034評論 25 709
  • 挑了今天4月1號,周五,應(yīng)該去墓園不會扎堆,出發(fā)吧。 女司機(jī)車技不堪,撩的是車,撩了一輛本田 CRV. 年輕小夫妻...
    xuanxueqing笑哈哈閱讀 150評論 0 0

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