初识 kotlin 创建一个 kotlinApp

    安装android studio 下载地址 https://developer.android.google.cn/studio/index.html(最新版3.1.1)

    配置 android 环境参考百度

    创建 android Project 或者 android Module

    安装 Kotlin 插件 File --> Settings --> Pulgins --> Kotlin --> Install

    配置 build.gradle (project) 

buildscript {
    ext.kotlin_version = '1.2.31'
//    ext.anko_version = '0.8.2'
    repositories {
        jcenter()
        google()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.0.1'

        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

配置 app build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

普通 java 文件转换成 Konlit 


生成后的文件

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val  a = findViewById <TextView>(R.id.content)
        a.text = "你好"
        content.text = "你好"//相当于两行代码
        content.setOnClickListener({Toast.makeText(content.context, content.text, Toast.LENGTH_SHORT).show()})//点击事件监听

    }
}


猜你喜欢

转载自blog.csdn.net/ff_hh/article/details/79925636