前言
最近的事情有点多,继上一篇Anko初探有一段时间了,本文将继续更新用Anko开发Android相关教程;关于Anko网上的资源有限,大部分是查看官网说明一点点摸索的,有需求的请查看官方地址:Anko,作者水平有限,难免有疏漏之处。欢迎留言探讨。
本文将介绍Anko的第一部分——Anko组件,分以下四部分讲解:
Intents
Dialogs and toasts
Logging
Resources and dimensions
在项目下的build.gradle中添加anko和kotlin支持
buildscript
{
ext.kotlin_version = '1.2.20'
ext.anko_version='0.10.4'
repositories
{
google()
jcenter()
}
dependencies
{
classpath 'com.android.tools.build:gradle:3.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects
{
repositories
{
google()
jcenter()
}
}
task clean(type: Delete)
{
delete rootProject.buildDir
}
//log
compile "org.jetbrains.anko:anko:$anko_version"
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
// Anko Commons
compile "org.jetbrains.anko:anko-commons:$anko_version"
compile "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
// Anko Layouts
compile "org.jetbrains.anko:anko-sdk25:$anko_version" // sdk15, sdk19, sdk21, sdk23 are also available
compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
// Coroutine listeners for Anko Layouts
compile "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
compile "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"
// Anko SQLite
compile "org.jetbrains.anko:anko-sqlite:$anko_version"
//Anko Coroutines
compile "org.jetbrains.anko:anko-coroutines:$anko_version"
使用本组件需要添加依赖支持,请检查环境配置中的步骤2,是否有下面的依赖库
compile "org.jetbrains.anko:anko-commons:$anko_version"
startActivity("id" to "5")
text ="通过传值获得内容为:"+ui.owner.intent.extras.get("id")
使用anko帮我们封装好的intent,简化开发步骤
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])
使用本组件需要添加依赖支持,请检查环境配置中的步骤2,是否有下面的依赖库
compile "org.jetbrains.anko:anko-commons:$anko_version"
compile "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
toast("Hi there!")
toast(R.string.message)
longToast("Wow, such duration")
使用时会有如下错误:
can't access class 'android.support.design.widget.Snackbar',check your module classpath for missing or conflicting dependencies
有知道原因的小伙伴可以私信或给我留言
alert("Hi, I'm Roy", "Have you tried turning it off and on again?")
{
yesButton { toast("Oh…") }
noButton {}
}.show()
val countries = listOf("Russia", "USA", "Japan", "Australia")
selector("Where are you from?", countries, { dialogInterface, i ->
toast("So you're living in ${countries[i]}, right?")
})
val dialog = progressDialog(message = "Please wait a bit…", title = "Fetching data"){
isIndeterminate=true
}
dialog.show()
日志组件,比较简单
使用本组件需要添加依赖支持,请检查环境配置中的步骤2,是否有下面的依赖库
compile "org.jetbrains.anko:anko-commons:$anko_version"
Anko组件——Resources and dimensions
使用本组件需要添加依赖支持,请检查环境配置中的步骤2,是否有下面的依赖库
compile "org.jetbrains.anko:anko-commons:$anko_version"
使用dp或sp标明属性的大小
view后跟lparams对view的属性进行描述
参考:Github下载地址