model下build.gradle
dependencies {
implementation "org.jetbrains.anko:anko-commons:$anko_version"
}
项目下build.gradle加入统一版本管理
ext.anko_version='0.10.8'
Anko全家桶有很多,按自己需求导入
dependencies {
// Anko全家桶 包含以下所有
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"
}
点击查看最新版本
普通情况
val intent = Intent(this,SomeOtherActivity::class.java)
intent.putExtra("id",5)
intent.setFlag(Intent.FLAG_ACTIVITY_SINGLE_TOP)
startActvitiy(intent)
使用Anko
startActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())
startActivity<SomeOtherActivity>("id" to 5)
startActivity<SomeOtherActivity>(
"id" to 5,
"city" to "Denpasar"
)
Toasts
toast("Hi there!")
toast(R.string.message)
longToast("Wow, such duration")
SnackBars
view.snackbar("Hi there!")
view.snackbar(R.string.message)
view.longSnackbar("Wow, such duration")
view.snackbar("Action, reaction", "Click me!") { doStuff() }
Alerts
alert("Hi, I'm Roy", "Have you tried turning it off and on again?") {
yesButton { toast("Oh…") }
noButton {}
}.show()
alert(Appcompat, "Some text message").show()
alert {
customView {
editText()
}
}.show()
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?")
})
Progress dialogs
val dialog = progressDialog(message = "Please wait a bit…", title = "Fetching data")
Trait-like style
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
}
}
android.util.Log | AnkoLogger |
---|---|
v() | verbose() |
d() | debug() |
i() | info() |
w() | warn() |
e() | error() |
wtf() | wtf() |
Logger object style
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!")
}
}
Colors
Function | Result |
---|---|
0xff0000.opaque | non-transparent red |
0x99.gray.opaque | non-transparent #999999 gray |
You can specify dimension values in dip (density-independent pixels) or in sp (scale-independent pixels): dip(dipValue) or sp(spValue). Note that the textSize property already accepts sp (textSize = 16f). Use px2dip and px2sp to convert backwards.
applyRecursively()
verticalLayout {
editText {
hint = "Name"
}
editText {
hint = "Password"
}
}.applyRecursively { view -> when(view) {
is EditText -> view.textSize = 20f
}}