Kotlin 基础(二)- DSL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Kotlin 基础(二)- DSL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4136字,纯文字阅读大概需要6分钟。
内容图文
![Kotlin 基础(二)- DSL](/upload/InfoBanner/zyjiaocheng/1107/5e450fbec3b546b884cb627c25c31dfe.jpg)
所谓 DSL 领域专用语言(Domain Specified Language/ DSL),其基本思想是“求专不求全”,不像通用目的语言那样目标范围涵盖一切软件问题,而是专门针对某一特定问题的计算机语言。
![技术分享图片](https://upload-images.jianshu.io/upload_images/14140248-37ac4694d54319ca.jpeg?imageMogr2/auto-orient/strip|imageView2/2/w/1024/format/webp)
Kotlin DSL 定义:使用 Kotlin 语言开发的,解决特定领域问题,具备独特代码结构的 API 。
一、DSL
DSL(domain specific language),即领域专用语言:专门解决某一特定问题的计算机语言,比如大家耳熟能详的 SQL 和正则表达式。使用DSL的编程风格,可以让程序更加简单干净、直观简洁。当然,我们也可以创建自己的 DSL。相对于传统的 API, DSL 更加富有表现力、更符合人类语言习惯。
如果为解决某一特定领域问题就创建一套独立的语言,开发成本和学习成本都很高,因此便有了内部 DSL 的概念。所谓内部 DSL,便是使用通用编程语言来构建 DSL,比如,Kotlin DSL。
1.1 常见的 DSL
常见的 DSL 在很多领域都能看到,例如:
- 软件构建领域 Ant
- UI 设计师 HTML
- 硬件设计师 VHDL
1.2 通用编程语言 vs DSL
通用编程语言(如 Java、Kotlin、Android等),往往提供了全面的库来帮助开发者开发完整的应用程序,而 DSL 只专注于某个领域,比如 SQL 仅支持数据库的相关处理,而正则表达式只用来检索和替换文本,我们无法用 SQL 或者正则表达式来开发一个完整的应用。
- DSL 供非程序员使用,供领域专家使用;
- DSL 有更高级的抽象,不涉及类似数据结构的细节;
- DSL 表现力有限,其只能描述该领域的模型,而通用编程语言能够描述任意的模型;
无论是通用编程语言,还是领域专用语言,最终都是要通过 API 的形式向开发者呈现。良好的、优雅的、整洁的、一致的 API 风格是每个优秀开发者的追求,而 DSL 往往具备独特的代码结构和一致的代码风格。
二、Kotlin DSL 在 Android 开发的应用
2.1 Anko
Anko 是一个 DSL (Domain-Specific Language), 它是 JetBrains 出品的,用 Kotlin 开发的安卓框架。它主要的目的是用来替代以前 XML 的方式来使用代码生成 UI 布局。
2.1.1 使用
Anko 中, 不需要继承其他奇怪的类,只要标准的 Activity, Fragment,FragmentActivity 或者其他任意的类
首先, 在使用 Anko 的 DSL 的类中导入 org.jetbrains.anko.* .
DSL 可以在 onCreate()中使用:
override fun onCreate(savedInstanceState: Bundle?) { super<Activity>.onCreate(savedInstanceState) verticalLayout { padding = dip(30) editText { hint = "Name" textSize = 24f } editText { hint = "Password" textSize = 24f } button("Login") { textSize = 26f } } }
不需要显示的调用 setContentView(R.layout.something), Anko 自动为 Activity(只会对Activity)进行 set content view
padding, hint 和 textSize 是 扩展属性. 大多数 View 具有这些属性,允许使用 text = “Some text” 代替 setText(“Some text”).
verticalLayout (一个竖直方向的 LinearLayout), editText 和 button
扩展函数. 这些函数存在与ANdroid 框架中的大部View中, Activities, Fragments ( android.support 包中的) 甚至 Context同样适用.
如果有一个 Context 实例, 可以写出下面的DSL结构:
val name = with(myContext) { editText { hint = "Name" } }
变量 name 成为了 EditText类型.
2.1.2 Helper 方法
你可能注意到了,前面 button 方法接了一个字符串参数,这样的Helper方法同样使用与 TextView, EditText, Button , ImageView.
如果你不需要 View 其他的属性,你可以省略 {} 直接写 button(“Ok”) 或只有 button():
verticalLayout { button("Ok") button("Cancel") }
2.1.3 Layouts 和 LayoutParams
在父布局中布局控件可能需要使用 LayoutParams.xml 中长这样:
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android_layout_marginLeft="5dip" android_layout_marginTop="10dip" android:src="@drawable/something" />
Anko 中, 在 View 的后面使用 lparams 来实现类似与 xml 的 LayoutParams。
linearLayout { button("Login") { textSize = 26f }.lparams(width = wrapContent) { horizontalMargin = dip(5) topMargin = dip(10) } }
如果指定了 lparams,但是没有指定 width 或者 height, 默认是 WRAP_CONTENT,但是你可以自己通过使用 named arguments指定.
注意下面一些方便的属性:
- horizontalMargin 同时设置 left 和 right margins
- verticalMargin 同时设置 top 和 bottom
- margin 同时设置4个方向的 margins
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent" android:layout_width="match_parent"> <EditText android:id="@+id/todo_title" android:layout_width="match_parent" android:layout_heigh="wrap_content" android:hint="@string/title_hint" /> <!-- Cannot directly add an inline click listener as onClick delegates implementation to the activity --> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/add_todo" /> </LinearLayout>
使用 Anko 之后,可以用代码实现布局,并且 button 还绑定了点击事件。
verticalLayout { var title = editText { id = R.id.todo_title hintResource = R.string.title_hint } button { textResource = R.string.add_todo onClick { view -> { // do something here title.text = "Foo" } } } }
可以看到 DSL 的一个主要优点在于,它需要很少的时间即可理解和传达某个领域的详细信息。
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) verticalLayout { padding = dip(30) editText { hint = "Name" textSize = 24f } editText { hint = "Password" textSize = 24f } button("Login") { textSize = 26f } } }
2.1.4 弹窗
alert("Hi, I‘m Roy", "Have you tried turning it off and on again?") { yesButton { toast("Oh…") } noButton {} }.show()
2.1.5 异步
doAsync { // Long background task uiThread { result.text = "Done" } }
关于我
更多信息可以点击关于我 , 非常希望和大家一起交流 , 共同进步
原文:https://www.cnblogs.com/1157760522ch/p/11557249.html
内容总结
以上是互联网集市为您收集整理的Kotlin 基础(二)- DSL全部内容,希望文章能够帮你解决Kotlin 基础(二)- DSL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。