前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android Kotlin启程之Anko

Android Kotlin启程之Anko

作者头像
码客说
发布2019-10-22 14:43:36
1K0
发布2019-10-22 14:43:36
举报
文章被收录于专栏:码客

前言

之前写了一写Kotlin的基本操作 是不是感觉太好用了

但是接下来介绍的这个库 让你会觉得太爽了 这个库就是超级好用的库anko

它总共有四个部分

  • Anko Commons: a lightweight library full of helpers for intents, dialogs, logging and so on;
  • Anko Layouts: a fast and type-safe way to write dynamic Android layouts;
  • Anko SQLite: a query DSL and parser collection for Android SQLite;
  • Anko Coroutines: utilities based on the kotlinx.coroutines library.

这里我们先说Anko Commons 它的作用

  • Intents (wiki);
  • Dialogs and toasts (wiki);
  • Logging (wiki);
  • Resources and dimensions (wiki).

异步操作

代码语言:javascript
复制
doAsync {
    //异步分线程操作
    uiThread {
        //主线程操作
    }
}

Intent

引用

代码语言:javascript
复制
dependencies {
    compile "org.jetbrains.anko:anko-commons:0.10.4"
}

原写法

代码语言:javascript
复制
var intent = Intent();
intent.putExtra("id",5)
intent.putExtra("name","zhangjian")
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
intent.setClass(this, SMainActivity::class.java)
startActivity(intent)

新写法

代码语言:javascript
复制
var intent = intentFor<SMainActivity>("id" to 5,"name" to "zhangjian").clearTask().newTask()
startActivity(intent)

其他有用的Intent

作用

写法

打电话

makeCall(number) without tel:

发短信

sendSMS(number, [text]) without sms:

打开网页

browse(url)

分享文字

share(text, [subject])

发邮件

email(email, [subject], [text])

参数 ([]) 是可选的. 如果intent成功发送 方法返回true.

Dialogs and toasts

引用

代码语言:javascript
复制
dependencies {
    compile "org.jetbrains.anko:anko-commons:0.10.4"
    compile "org.jetbrains.anko:anko-design:0.10.4" // For SnackBars
}

使用方式

Toasts

Simply shows a Toast message.

代码语言:javascript
复制
toast("Hi there!")
toast(R.string.message)
longToast("Wow, such duration")

SnackBars

Simply shows a SnackBar message.

代码语言:javascript
复制
snackbar(view, "Hi there!")
snackbar(view, R.string.message)
longSnackbar(view, "Wow, such duration")
snackbar(view, "Action, reaction", "Click me!") { doStuff() }

Alerts

A small DSL for showing alert dialogs.

代码语言:javascript
复制
alert("Hi, I'm Roy", "Have you tried turning it off and on again?") {
    yesButton { toast("Oh…") }
    noButton {}
}.show()

The code above will show the default Android alert dialog. If you want to switch to the appcompat implementation, use the Appcompat dialog factory:

代码语言:javascript
复制
alert(Appcompat, "Some text message").show()

Android and Appcompat dialog factories are included by default, but you can create your custom factories by implementing the AlertBuilderFactory interface.

alert() functions seamlessly support Anko layouts as custom views:

代码语言:javascript
复制
alert {
    customView {
        editText()
    }
}.show()

Selectors

selector() shows an alert dialog with a list of text items:

代码语言:javascript
复制
val countries = listOf("Russia", "USA", "Japan", "Australia")
selector("Where are you from?", countries, { dialogInterface, i ->
    toast("So you're living in ${countries[i]}, right?")
})

Progress dialogs

progressDialog() creates and shows a progress dialog.

代码语言:javascript
复制
val dialog = progressDialog(message = "Please wait a bit…", title = "Fetching data")

Logging

引用

代码语言:javascript
复制
dependencies {
    compile "org.jetbrains.anko:anko-commons:0.10.4"
}

使用方式

代码语言:javascript
复制
info("London is the capital of Great Britain")
debug(5) // .toString() method will be executed
warn(null) // "null" will be printed

对比

android.util.Log

AnkoLogger

v()

verbose()

d()

debug()

i()

info()

w()

warn()

e()

error()

wtf()

wtf()

默认的tag是类名 Lambda result will be calculated only if Log.isLoggable(tag, Log.INFO) is true.

自定义tag

代码语言:javascript
复制
class SomeActivity : Activity() {
    private val log = AnkoLogger<SomeActivity>(this)
    private val logWithASpecificTag = AnkoLogger("my_tag")

    private fun someMethod() {
        log.warning("Big brother is watching you!")
    }
}

Resources and dimensions

这部分总体感觉没什么卵用

Colors

Two simple extension functions to make the code more readable.

Function

Result

0xff0000.opaque

non-transparent red

0x99.gray.opaque

non-transparent #999999 gray

Dimensions

px转dp/sp

代码语言:javascript
复制
px2dip(10)
px2sp(10)

applyRecursively()

applyRecursively() applies the lambda expression to the passed View itself, and then recursively to every child of a View if it is a ViewGroup:

代码语言:javascript
复制
verticalLayout {
    editText {
        hint = "Name"
    }
    editText {
        hint = "Password"
    }
}.applyRecursively { view -> when(view) {
    is EditText -> view.textSize = 20f
}}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-03-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 异步操作
  • Intent
  • Dialogs and toasts
    • Toasts
      • SnackBars
        • Alerts
          • Selectors
            • Progress dialogs
            • Logging
            • Resources and dimensions
              • Colors
                • Dimensions
                  • applyRecursively()
                  相关产品与服务
                  日志服务
                  日志服务(Cloud Log Service,CLS)是腾讯云提供的一站式日志服务平台,提供了从日志采集、日志存储到日志检索,图表分析、监控告警、日志投递等多项服务,协助用户通过日志来解决业务运维、服务监控、日志审计等场景问题。
                  领券
                  问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档