将Anko应用到项目中
来源:互联网 发布:云宝网络 编辑:程序博客网 时间:2024/06/11 00:02
Anko是JetBrains开发的一个强大的库。让android开发更加快速和容易。它可以简化你的代码,使其易读。
Anko包含几个部分:
- Anko Commons:一个轻量的工具包,用来操作intent,dialog,log等等
- Anko Layouts:一种快速安全的方法,用来动态生成布局
- Anko SQLite:
- Anko Coroutines:基于kotlinx.coroutines的libary
这里只对Anko Commons进行介绍。
1、扩展
1.1扩展函数是指在一个类上增加一个新的行为,甚至我们不需要获得这个类的权限。
举个例子,我们平时用的showToast方法。在java的写法如下:
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 virtuallyC().caller(D1())// prints "D.foo in C" - extension receiver is resolved statically
2、Anko Commons
a.操作IntentstartActivity(intentFor<SomeOtherActivity>("id" to 5).singleTop())startActivity<SomeOtherActivity>("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<SomeActivity>(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//px2spfun 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 }}
- 将Anko应用到项目中
- 将Ajax应用到项目中
- 将Bootstrap应用到Aurelia项目中
- druid 应用到项目中
- 将项目发布到Cloud Foundry应用中使用bx app push过程中报错
- 将Windows 7应用到专用设备中
- 将网页嵌入到android应用中
- 将网页嵌入到android应用中
- 将网页嵌入到android应用中
- 将网页嵌入到android应用中
- 将网页嵌入到android应用中
- 将网页嵌入到android应用中
- Anko
- 将dspace整合到java项目中
- 将MyEclipse项目导入到Eclipse中
- 将MyEclipse项目导入到Eclipse中
- 如何将EXT添加到项目中
- 将eclipse项目转移到Androidstudio中
- JSch 指令不识别
- Spring源码学习
- C++中成员变量加上static或const关键字后的分析
- python的虚拟环境应用
- Java中异常的抛出原则
- 将Anko应用到项目中
- Entrance and Departure Detection Base on BLE Beacon RSSI
- HDU 3954 Level up (线段树)
- 让IE6/IE7/IE8支持CSS3的8种方法
- 前端跨域请求解决方案
- 《java并发编程实战》笔记(第6章)
- Java基础——MD5处理、加密、解密代码
- caffe常见错误
- #742&743 –使用触摸事件旋转和缩放控件