Anko
Anko是一个Kotlin编写的为了快速/易懂的开发Android的库,一共有四个部分,分别为Commons,Layouts,SQLite,Coroutines
Commons:简单的使用Toast、Intent、Dialog等
Layouts:可以动态编写Android布局
SQLite:SQlite封装
Coroutines:基于kotlinx.coroutines封装的协程库
引入
全部引入
implementation "org.jetbrains.anko:anko:$anko_version"
分模块引用
// Anko Commons implementation "org.jetbrains.anko:anko-commons:$anko_version" // Anko Layouts implementation "org.jetbrains.anko:anko-sdk25:$anko_version" // sdk15, sdk19, sdk21, sdk23 are also available implementation "org.jetbrains.anko:anko-appcompat-v7:$anko_version" // Coroutine listeners for Anko Layouts implementation "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version" implementation "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version" // Anko SQLite implementation "org.jetbrains.anko:anko-sqlite:$anko_version"
anko-commons
Colors:感觉没啥用
Function | Result |
---|---|
0xff0000.opaque | non-transparent red |
0x99.gray.opaque | non-transparent #999999 gray |
Dimensions:快捷转换尺寸
dip() dp->px sp() sp->px px2dip() px->dp px2sp() px->sp
applyRecursively:修改某个View下面所有的子View的属性,也可以指定条件
//将root_layout下面的所有TextView的文字改成 "被修改了" root_layout.applyRecursively { if (it is TextView) it.text = "被修改了" }
log:日志打印
//方法一 直接实现AnkoLogger接口 class SomeActivity : Activity(), AnkoLogger { private fun someMethod() { info("London is the capital of Great Britain") debug(5) // .toString() method will be executed warn(null) // "null" will be printed } } //方法二:指定log对象 class SomeActivity : Activity() { private val log = AnkoLogger(this.javaClass) private val logWithASpecificTag = AnkoLogger("my_tag") private fun someMethod() { log.warning("Big brother is watching you!") } }
Toast:显示Toast
toast("Hi there!") toast(R.string.message) longToast("Wow, such duration")
显示Dialog
view.snackbar("Hi there!") view.snackbar(R.string.message) view.longSnackbar("Wow, such duration") view.snackbar("Action, reaction", "Click me!") { doStuff() } alert("Hi, I'm Roy", "Have you tried turning it off and on again?") { yesButton { toast("Oh…") } noButton {} }.show() progressDialog(message = "Please wait a bit…", title = "Fetching data")
创建Intent
//创建Intent对象并传递参数 intentFor<SomeOtherActivity>("id" to 5) //可以通过action等设置其他属性
几个系统封装
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]) |
Coroutines :执行异步操作
doAsync { //new thread uiThread { //ui线程 } }
多写一点
由于Anko中的Layouts以及SQLite对于我来说并不是那么需要,所以就省略了,更多可以查看git文档