您的当前位置:首页正文

Anko之Anko Commons(二)

2024-11-09 来源:个人技术集锦
  • Intents (wiki);
  • Dialogs and toasts (wiki);
  • Logging (wiki);
  • 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
  • 如果有赞赏当然就更好了,是给我的肯定和前进的动力。
显示全文