带你全方位使用Anko库-下篇
目前anko库已更新到了0.10.8版本,这一年来的升级主要是在适配安卓版本和IDE版本以及kotlin版本上,功能并没有多少亮眼的改动。说实话,anko库野心不小,功能越来越多,但是感觉使用起来相对较杂乱,又没有很详细的文档来说明一些亮点技术的使用,上手后难免会宝山空归。。 by 2018-11-28
目前demo工程也已升级到新版本。
kotlin的伴生库-anko迭代了已经多个版本了,但是目前网上的资源主要集中在官方说明的机械翻译或者旧博客的复制/粘贴上。
本文带大家全方位的熟悉anko库的使用。anko库目前主要有四个核心库
dependencies {
//anko_version脚本配置,下同,本文中,该值为0.10.8
compile "org.jetbrains.anko:anko-commons:$anko_version"
}
dependencies {
// Anko Layouts
compile "org.jetbrains.anko:anko-sdk25:$anko_version" // sdk15, sdk19, sdk21, sdk23 are also available
compile "org.jetbrains.anko:anko-appcompat-v7:$anko_version"
// 主要为兼容一些控件事件的协程,不过协程coroutines目前还不是kotlin的正式内容
compile "org.jetbrains.anko:anko-sdk25-coroutines:$anko_version"
compile "org.jetbrains.anko:anko-appcompat-v7-coroutines:$anko_version"
}
dependencies {
compile "org.jetbrains.anko:anko-coroutines:$anko_version"
}
dependencies {
compile "org.jetbrains.anko:anko-sqlite:$anko_version"
}
本篇主要介绍commons库和layout库的使用
//启动
startActivity("name" to "小明", "age" to 12)
//IntentActivity接收参数
var name = intent.extras.getString("name")
var age = intent.extras.getInt("age")
//若有其它设置,则&intentFor方法&构建intent
startActivity(intentFor("name" to "小红", "age" to 13).singleTop())
browse("http://m.baidu.com")
verbose("tag-默认为调用类")//貌似不会打印,原因暂时未知
debug(110)//貌似不会打印,原因暂时未知
warn(null)
info(listOf("today", "is", "a", "fine", "day"))
error(HashMap().apply {
put("小明", "12")
put("小红", "13")
})
warn(CData("data", 100))
...
这里可结合一种优雅打log的方式,定制一份很强大的log方式,记得模板要声明kotlin的applicable,比如
error("$method$($file$:$line$)\r\n -"+$text1$)
dip(100)//dp->px
px2dip(100)//px->dp
//实用的attempt函数,{}若正常执行,则value返回{}的返回值,
//若{}执行有异常,并不会闪退,只是会设置个error属性。很好的try..catch替代方案
attempt { 3 }.value//结果3
attempt { 1 / 0 }.error
//还有sdk版本相关的
doFromSdk(21) {
info("从api 21开始打印")
}
doIfSdk(21) {
//获得版本名也简单的多
packageManager.getPackageInfo(packageName, 0).versionName
info("只有api 21才打印")
}
alert("this is the msg") {
customTitle {
verticalLayout {//用到了anko-layout库
imageView(R.mipmap.ic_launcher)//方便的设置内容
editText { hint = "hint_title" }
}
}
okButton { toast("button-ok") }
cancelButton { toast("button-cancel") }
}.show()
val countries = listOf("Russia", "USA", "England", "Australia")
selector("Where are you from?", countries) { ds, i ->
toast("So you're living in ${countries[i]}, right?")
}
–其它的一些常用属性,代码能简洁不少,%Context/Fragment类扩展函数,有的是AnkoContext的类扩展属性%
displayMetrics,defaultSharedPreferences,act
assets,ctx,contentView,resources//基本见名知义吧,不介绍了
bundleOf("name" to "test")//just show bundle,return Bundle对象
toast(".."'),progressDialog()..//使用很简单
以上,commons库的使用介绍完毕,应该是比较全了。
下面介绍layout库,网上有关这个的库的介绍比较多,主要是dsl比较cool比较潮吧,不过目前这个对简答的布局比较适用,复杂的还真不好说。权当语法糖熟悉下。这里尽量只列代码和使用,还有一些网上少见的用法。
verticalLayout {
padding = dip(30)
editText {
hint = "Name"
textSize = 24f
}
editText {
hint = "Password"
textSize = 24f
}
button("Login") {
textSize = 26f
id = BTN_ID
}
}
//note
//id findview
find
//声明一个类继承AnkoComponent,对应泛型类到一个LayShowActivity,然后布局
class LayoutActyUI : AnkoComponent {
val ET_ID = 0x1001
override fun createView(ui: AnkoContext) = with(ui) {
verticalLayout {
val name = editText("LayoutActyUI") {
id = ET_ID
}
button("Say Hello") {
onClick {
ctx.toast("Hello, ${name.text}!")
name.textColor = 0xffff0000.toInt()
}
}
}
}
}
//然后在LayShowActivity调用下面的方法即可实现加载布局
LayoutActyUI().setContentView(this)
verticalLayout {
button("seekbar") {
textSize = 26f
}.lparams(width = wrapContent) {//布局参数
horizontalMargin = dip(15)
topMargin = dip(20)
}
editText {
hintResource = R.string.app_name//直接引用资源文件内容的方式
textChangedListener {多函数事件简单写法
onTextChanged { str, start, before, count ->
ai(str)
}
}
}
}
这里补充下函数控件事件监听器,比如:
//原java方式用法,必须传入TextWatcher对象作为参数,而且有些函数根本用不上,比较麻烦。
EditText(act).addTextChangedListener(object :TextWatcher{
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
})
//现在只需利用anko库的这个写法,使代码大大减少且清晰
EditText(act).textChangedListener {
onTextChanged { str, start, before, count ->
ai(str)//log..info
}
}
onClick{},onCheckedChange{},onDateChange{}
onDrawerOpen{},onItemClick{},onScrollChange{}等等都有类似用法
好了,本篇就介绍到这,下篇会对另两个子库做个比较全面的介绍。
附上demo工程链接。
作者刘咸尚