Kotlin 我的第一个kotlin程序【Android Studio】

这几天被安卓2017年开发者大会刷屏了
Google宣布Kotlin为新的Android一级开发语言

Kotlin 是一个基于 JVM 的新的编程语言,由 JetBrains 开发。
Kotlin可以编译成Java字节码,也可以编译成JavaScript,方便在没有JVM的设备上运行。
JetBrains,作为目前广受欢迎的Java IDE IntelliJ 的提供商,在 Apache 许可下已经开源其Kotlin 编程语言。
Kotlin已正式成为Android官方开发语言。

列举三种可以进行kotlin开发的IDE 【Android Studio】【IntelliJ IDEA】【Eclipse】
下面我们用【Android Studio】来开始我们的Kotlin开发之旅(入门篇)
goole开发者官网
https://developers.google.cn
如果您使用Android Studio 官方 Android IDE
https://developer.android.google.cn/studio/index.html
教程
http://kotlinlang.org/docs/tutorials/kotlin-android.html
中文网:https://www.kotlincn.net/docs/reference/

如果您使用IntelliJ IDEA 教程:
http://kotlinlang.org/docs/tutorials/getting-started.html
如果您使用Eclipse 教程:
http://kotlinlang.org/docs/tutorials/getting-started-eclipse.html

插件配置:
1.   cmd加逗号


![配置插件.png](http://upload-images.jianshu.io/upload_images/3729815-73ce6f1e0c98fc15.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/640)


![下载配置插件.png](http://upload-images.jianshu.io/upload_images/3729815-bced92556ddfa356.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/640)

之后重启Android Studio
把代码转化成kotlin代码
![转换代码.png](http://upload-images.jianshu.io/upload_images/3729815-6a0205eef6dde077.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/640)

![转换后.png](http://upload-images.jianshu.io/upload_images/3729815-0438ab5adabbca48.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

![配置一下](http://upload-images.jianshu.io/upload_images/3729815-546334dbabda37c2.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/640)
点击同步一下库文件,会从服务器上下载并安装
![点击同步一下库文件](http://upload-images.jianshu.io/upload_images/3729815-156e6d077dcd0239.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/640)

至此就配置完成kotlin环境 下边就可以开始写代码了:
可以参考教程:http://kotlinlang.org/docs/tutorials/kotlin-android.html
下边安装下扩展Kotlin Android Extensions

Kotlin Android Extensions
把这一句
apply plugin: 'kotlin-android-extensions'

![粘贴.png](http://upload-images.jianshu.io/upload_images/3729815-f15a8ff49a175ff6.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/640)


![](http://upload-images.jianshu.io/upload_images/3729815-7c7f4c89370160a8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/840)

package com.example.douxindong.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.webkit.WebViewClient
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

//只写这一句代码 根据ID取出myTextView这个控件,直接赋值
myTextView.text = "我在这里"

}

}

效果:

![](http://upload-images.jianshu.io/upload_images/3729815-529943e3527e6a08.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

下一篇[Kotlin程序加载webView(Kotlin编写第一个WebView浏览器App)](http://www.jianshu.com/p/c390412daf2b)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容