原文地址:https://www.kotlindevelopment.com/why-should-use-anko/

Anko是一个用Kotlin编写的Android库并且由Jetbrains来维护的。其目的是通过Kotlin的功能来提高Android开发的速度,从而使开发更加方便,让我们看一下Anko给我们提供了哪些好的工具:

Anko有四个方面的模块:

  • Commons
  • Layouts
  • SQLite
  • Coroutines

Commons提供了各种各样的帮助功能和分支,使用Layouts模块可以使用Anko DSL语言从Kotlin创建UI,SQLite会使操作Sqlite更简单,最后,Anko为Kotlin提供了一些帮助程序。

开发Android应用程序从未如此简单

在这篇文章中,我主要关注Common模块

Anko Commons

最常用的公共部分

让我们从一些最基本的简化操作开始吧,在Android中View.setOnclickListener被用在很多地方,如果我们能使其更简洁就更好了,下面是我们平常使用的用法:

button.setOnClickListener(object : View.OnClickListener{ override fun onClick(v: View) { }})

但Anko帮助我们尽量减少我们所做的工作

button.onClick{ }
Intent

Intent是你作为Android开发者要学的第一件事之一,这里提供了更简便的用法:

val intent = Intent(this, MainActivity::class.java)intent.putExtra("id", 5)intent.putExtra("name", "John")startActivity(intent)

让我们来看一下怎么使他更简单:

startActivity("id" to 5, "name" to "John")

一行代码代替五行。
Anko还提供了一些常用意图的用法

browse("https://makery.co")share("share", "subject")email("hello@makery.co", "Great app idea", "potato")

Anko还提供了更易于使用的对话框API,不需要使用Builder模式。
常见的用法:

val builder = AlertDialog.Builder(this)builder.setTitle("Java")builder.setMessage("Java is… old!")builder.setPositiveButton("OK") { dialog, which -> toast("Yay!") }builder.setNegativeButton("Cancel") { dialog, which -> toast("What?") }builder.show()

Anko的用法:

alert(Appcompat,"Kotlin","Kotlin is so fresh!"){  customeView{editText()}  positiveButton("OK"){toast("Yay!")}  negativeButton("Cancel"){toast("What?")}}.show()

Size 问题

老的Size的转换的代码

val dpAsPx = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, getResources().getDisplayMetrics())

Anko的用法

val dpAsPx = dip(10f)sp(15f)

Api level 23

碎片化是每个Android开发人员面临的问题,所以我们希望使用最新版本中Android的酷炫的功能,所以,就需要对不同的版本加上判断:

if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP){ }if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){ }

使用Anko

doIfSdk(Build.VERSION_CODES.LOLLIPOP){ }doFromSdk(Build.VERSION_CODES.LOLLIPOP){ }

Snack的使用

Android snackbar也可以更好,你有没有在使用Snackbar的时候忘记调用show方法,从而调试半天的经历?

Snackbar.make(findViewById(android.R.id.content), "This is a snack!", Snackbar.LENGTH_LONG).show()

Anko用法,不需要调用show方法

longSnackbar(findViewById(android.R.id.content), "This is a snack!")

如果你是Toast的粉丝的话:

toast("Message")

线程通讯

处理多线程并不简单,但是在开发中他是一个很常见的模式,大多数时候我们要从UI线程中卸载工作,但是Anko提供的方法却是非常简单的。

doAsync { //IO task or other computation with high cpu load uiThread {   toast("async computation finished") }}

添加Anko到你的项目中

如果你喜欢上面的东西,那还等什么,赶快添加Anko到你的项目中吧

ankoVersion = "0.10.1"dependencies { compile "org.jetbrains.anko:anko-appcompat-v7-listeners:$ankoVersion" compile "org.jetbrains.anko:anko-design-listeners:$ankoVersion" compile "org.jetbrains.anko:anko-design:$ankoVersion" compile "org.jetbrains.anko:anko-sdk15-listeners:$ankoVersion" compile "org.jetbrains.anko:anko-sdk15:$ankoVersion"}

findViewById

你听说过Kotlin扩展Gradle插件吗,您可以通过在Gradle脚本中引入一个额外的行来摆脱所有讨厌的findViewById()调用和投射视图:

apply plugin: 'kotlin-android-extensions'

现在你就可以使用id来引用view

  
import kotlinx.android.synthetic.main.activity_main.*...override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) button.onClick {  }}

更多相关文章

  1. Android(安卓)8.0 Oxxxx
  2. 关于android相机开发中遇到的内存溢出的问题
  3. Android(安卓)实现屏幕底部弹出Dialog
  4. Android简明开发教程十:数据绑定Data Binding
  5. cocos2d win7 安卓环境配置开发
  6. Android(安卓)RemoteViews(Android开发艺术随笔)
  7. Android开发:自由选择TextView的文字
  8. Android应用程序开发之requestWindowFeature()
  9. Android(安卓)Netd介绍

随机推荐

  1. Android(安卓)BroadCastReciver学习笔记
  2. android Q
  3. 《Android音频焦点机制深入理解》
  4. Android: Actions for BroadcastReceiver
  5. Android(安卓)Interprocess Communicatio
  6. android屏幕截图
  7. android scrollrefreshlist
  8. Android(安卓)Q 测试版
  9. 星星CheckBox按钮
  10. android TextSwitcher