首页 > 系统 > Android > 正文

学习的步伐(一)Kotlin Primer编程之android简单项目

2019-11-09 18:32:59
字体:
来源:转载
供稿:网友

Kotlin相关资料:

原文

网址: https://kotlinlang.org/docs/reference/

代码库: https://github.com/JetBrains/kotlin-web-site

中文翻译

网址: http://www.liying-cn.net/kotlin/docs/reference/

代码库: https://github.com/LiYing2010/kotlin-web-site

心灵鸡汤来了——学习是一种自我的坚持

摘要

Kotlin 是一个基于 JVM 的新的编程语言,由 JetBrains 开发。

其主要设计目标:

创建一种兼容 java 的语言

让它比 Java 更安全,能够静态检测常见的陷阱。如:引用空指针

让它比 Java 更简洁,通过支持 variable type inference,higher-order functions (closures),extension functions,mixins and first-class delegation 等实现。

让它比最成熟的竞争对手 Scala 语言更加简单。

好处:

作为一个跨平台的语言,Kotlin 可以工作于任何 Java 的工作环境:服务器端的应用,移动应用(Android版),桌面应用程序。Kotlin能够完美兼容现有的Java代码, 像我们平时使用的Volley、OKHttp等开发框架,在Kotlin语言下依旧能正常运行。

Kotlin确实能省略许多的代码编写,以及它特有的语言特性。

Kotlin 中没有基础类型,数组是定长的,泛型是安全的,即便运行时也是安全的,重要的是 Java 和 Kotlin 之间的互操作性:Kotlin 可以调用 Java,反之亦可。

准备工作

首先下载相关的Kotlin插件

Kotlin语言支持包

Kotlin Extensions For Android是我们稍后会用到的特性的增强包。

这里写图片描述

下载后重启Android Studio

Hello Kotlin

A. 正常创建一个Android项目

B. 然后选中Code—> Convert Java File To Kotlin File

这里写图片描述

选中后:

这里写图片描述

C. 点击Tool->Kotlin->Configure Kotlin in PRoject

这里写图片描述

选中Ok后,在app的bulid.gradle会出现这些配置:

/**这些事系统默认帮我们配置的,无需操心*/apply plugin: 'kotlin-android'compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

xml文件还是跟正常android一样:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="zlg.wolf.kotlin.MainActivity"> <TextView android:textSize="30dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello Kotlin!" /></RelativeLayout>

运行效果:

这里写图片描述

这些就是Kotlin的基本使用! 后续会持续学习,欢迎沟通交流!


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表