Anko-commons基本使用



导入


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.LogAnkoLogger
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

FunctionResult
0xff0000.opaquenon-transparent red
0x99.gray.opaquenon-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
}}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值