承接上一部分的anko layout,元旦過(guò)后,給大家來(lái)一波知識(shí),恢復(fù)一下coding...;上一部分地址anko_layout.
文本來(lái)源
英文鏈接 Anko Commons is a "toolbox" for Kotlin Android developer. The library contains a lot of helpers for Android SDK, including, but not limited to:
1. Intents (wiki);
2. Dialogs and toasts (wiki);
3. Logging (wiki);
4. Resources and dimensions (wiki).
anko Commons作為Android Kotlin發(fā)展的一個(gè)工具盒,這個(gè)依賴包在Android SDK中包含了許多好處
Intents(用于頁(yè)面跳轉(zhuǎn)的意圖)
1. 加入需要的依賴
dependencies {
compile "org.jetbrains.anko:anko-commons:版本號(hào)"
}
2. 以前原生Kotlin的寫法
val intent = Intent(this, SomeOtherActivity::class.java)
intent.putExtra("id", 5)
intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActivity(intent)
3. 現(xiàn)在的簡(jiǎn)寫
startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
如果參數(shù)少菇存,還可以更簡(jiǎn)單的寫:
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.
Dialogs and toasts
>1. 第一步同樣導(dǎo)入依賴
dependencies {
compile "org.jetbrains.anko:anko-commons:$anko_version"
compile "org.jetbrains.anko:anko-design:$anko_version" // For SnackBars
}
2. Toast簡(jiǎn)寫,更加方便簡(jiǎn)潔
toast("直接填寫需要的內(nèi)容")//默認(rèn)是短按彈出Toast
toast(R.string.message)//也可以使用資源文件中的文本內(nèi)容
longToast("長(zhǎng)按彈出提示內(nèi)容")
3. SnackBars的使用(android 5.0還是6.0的一個(gè)新的屬性垒手,類似Toast效果践磅,但是更加強(qiáng)大的顯示)必須引入材料設(shè)計(jì)的依賴:
compile 'com.android.support:design:sdk版本號(hào)'
//view 代表是在哪個(gè)控件之下顯示按傅。
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(對(duì)話框)
alert ("標(biāo)題(title))", "內(nèi)容(content)"){
yesButton {}//確定按鈕
noButton {}//取消按鈕
}.show()//系統(tǒng)默認(rèn)的
5. 自定義的對(duì)話框梆暮,包括自定義title
alert {
customView {
editText()
}
customTitle {
}
}.show()
6. Progress dialog(進(jìn)度條的對(duì)話框)
val dialogPress = ProgressDialog(activity)
7. Selectors(多條目的點(diǎn)擊事件彈出框)
val countries = listOf("Russia", "USA", "Japan", "Australia")
selector("Where are you from?", countries, { dialogInterface, i ->
toast("So you're living in ${countries[i]}, right?")
})
Logging
日志打印anko使用的是 AnkoLogger
必須要繼承 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<當(dāng)前Activity>(this)
private val logWithASpecificTag = AnkoLogger("my_tag")//設(shè)置特別的標(biāo)志
private fun someMethod() {
log.warning("Big brother is watching you!")
}
Resources and dimensions
對(duì)于此部分到此結(jié)束7!啦粹!
《Anko之Anko SQLite(三)》敬請(qǐng)期待...
歡迎評(píng)閱偿荷!
郵箱:simoncqhy@163.com
如果有贊賞當(dāng)然就更好了,是給我的肯定和前進(jìn)的動(dòng)力唠椭。