Anko之Anko Commons(二)

承接上一部分的anko layout,元旦过后,给大家来一波知识,恢复一下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发展的一个工具盒,这个依赖包在Android SDK中包含了许多好处

Intents(用于页面跳转的意图)

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. 现在的简写
   startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
   如果参数少,还可以更简单的写:
     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. 第一步同样导入依赖
  dependencies {
        compile "org.jetbrains.anko:anko-commons:$anko_version"
         compile "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
}
2. Toast简写,更加方便简洁
    toast("直接填写需要的内容")//默认是短按弹出Toast
    toast(R.string.message)//也可以使用资源文件中的文本内容
    longToast("长按弹出提示内容")
3. SnackBars的使用(android 5.0还是6.0的一个新的属性,类似Toast效果,但是更加强大的显示)必须引入材料设计的依赖:
     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 ("标题(title))", "内容(content)"){
             yesButton {}//确定按钮
             noButton {}//取消按钮
         }.show()//系统默认的
5. 自定义的对话框,包括自定义title
     alert {
             customView {
                 editText()
             }
             customTitle { 
             }
         }.show()
6. Progress dialog(进度条的对话框)
    val dialogPress = ProgressDialog(activity)
7. Selectors(多条目的点击事件弹出框)
    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<当前Activity>(this)
     private val logWithASpecificTag = AnkoLogger("my_tag")//设置特别的标志
     private fun someMethod() {
        log.warning("Big brother is watching you!")
     }

Resources and dimensions

资源文件

    对于此部分到此结束!!!
   《Anko之Anko SQLite(三)》敬请期待...
    欢迎评阅!
    邮箱:simoncqhy@163.com
    如果有赞赏当然就更好了,是给我的肯定和前进的动力。
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容