承接上一部分的anko layout,元旦事后,给你们来一波知识,恢复一下coding...;上一部分地址anko_layout.java
文原本源 英文连接 Anko Commons is a "toolbox" for Kotlin Android developer. The library contains a lot of helpers for Android SDK, including, but not limited to:react
anko Commons做为Android Kotlin发展的一个工具盒,这个依赖包在Android SDK中包含了许多好处android
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.
复制代码
>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?")
})
复制代码
日志打印anko使用的是 AnkoLoggergit
必需要继承 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!")
}
复制代码
资源文件github