承接上一部分的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