Anko是JetBrains开发的一个强大的库。让android开发更加快速和容易。它可以简化你的代码,使其易读。
Anko包含几个部分:
这里只对Anko Commons进行介绍。
public static void showToast(Context context, int message, int duration) {
Toast.makeText(context, message, duration).show();
}
在Kotlin的写法如下:
fun Context.showToast(message: String, duration: Int = Toast.LENGTH_SHORT) {
Toast.makeText(this, message, duration).show()
}
如果是java的写法,那么调用的方式一般是XXX.showToast(context, message, duration)。如果是Kotlin,那么这个方法于所有Context的子类都可以去调用。简便性,显而易见。
fun Int.dp(context: Context): Int {
if (context == null)
return (this * 1.5).toInt()
val scale = context.resources.displayMetrics.density
return (this * scale + 0.5).toInt()
}
dp在java代码中的使用,可以这么写10.dp(context).
以上只是一个简单的例子,Anko将这个优势发挥极致,提供了很多很好用的方法在Anko Commons中。
1.2
扩展属性更是牛逼啊。。。
我们可以随便点开一个我们的业务model,如Track多的有十几二十个属性,而其实很多都是并不通用,可能我就这个页面用到,所以给他新增进去,一来一往,就有很多了。今天借助Kotlin,我们将有怎么样的效果。比如我这个会员页,希望知道声音是否是会员属性
val Track.isNeedVip: Boolean
get() = isPaid && !UserInfoMannage.getInstance().user.isVip()
这样子,根本不需要往Track里面去塞新的字段,就能实现我们要的功能。虽然无法参与Gson解析,但是通过手动解析完全没有任何问题。(但也不一定,setter方法可能可以处理下,我还没试过)
1.3难点
Extensions declared as members can be declared as open and overridden in subclasses. This means that the dispatch of such functions is virtual with regard to the dispatch receiver type, but static with regard to the extension receiver type.
openclass D {
}
class D1 : D() {
}
openclass C {
openfun D.foo() {
println("D.foo in C")
}
openfun D1.foo() {
println("D1.foo in C")
}
fun caller(d: D) {
d.foo() // call the extension function
}
}
class C1 : C() {
overridefun D.foo() {
println("D.foo in C1")
}
overridefun D1.foo() {
println("D1.foo in C1")
}
}
C().caller(D())// prints "D.foo in C"
C1().caller(D())// prints "D.foo in C1" - dispatch receiver is resolved virtually
C().caller(D1())// prints "D.foo in C" - extension receiver is resolved statically
a.操作Intent
startActivity(intentFor("id" to 5).singleTop())
startActivity("id" to 5)
browse(url)
b.使用Dialog
toast("你好")
alert("Warining", "Are U sure?") {
yesButton { toast("Ok…") }
noButton {}
}.show()
val countries = listOf("China", "USA", "UK", "Australia")
selector("Where are you from?", countries) { i ->
toast("So you're living in ${countries[i]}, right?")
}
val dialog = progressDialog(message = "Please wait a bit…", title = "Fetching data")
c. 打log
使用AnkoLogger可以很方便的打log。
class SomeActivity : Activity() {
private val log = AnkoLogger(this)
private val logWithASpecificTag = AnkoLogger("my_tag")
private fun someMethod() {
log.warning("Big brother is watching you!")
}
}
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
}
}
//d.资源处理
//Dimensions
//dip(dipValue)
//sp(spValue)
//px2dip
//px2sp
fun Context.dip(value: Int): Int = (value * resources.displayMetrics.density).toInt()
//Colors
//0xff0000.opaque将16进制值转换成color的int类型
// ApplyRecursively
View.applyRecursively { view -> when(view) {
is EditText -> view.textSize = 20f
}}