Kotlin + Android Studio 的基本使用步骤

开发工具  Android Studio 3.1  

1,    创建工程时, 必须勾选  Include Kotlin SupportKotlin + Android Studio 的基本使用步骤

 2、打开项目的build.gradle,补充添加anko的版本号声明,以及Kotlin扩展库的路径,完整的编译配置如下所示:


buildscript {
    ext.kotlin_version = '1.2.30'//指定kotlin的编译版本号
    ext.anko_version = '0.9'//指定Anko 库的版本号
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.1.0'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

3, 打开模块的build.gradle,在文件开头补充添加Kotlin的扩展插件,配置添加如下:

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

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.example.administrator.mykotlin"
        minSdkVersion 16
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    implementation"org.jetbrains.anko:anko-common:$anko_version"
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

4, 页面中的内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/tv_id"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Kotlin 点击事件监听器"
        android:id="@+id/but_id"
        />

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Kotlin 长按事件监听器"
        android:id="@+id/but_long_id"
        />


</LinearLayout>

4,  Activity 中的代码

package com.example.administrator.mykotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
//TODO 引进Kotlin 的控件变量自动映射功能, 初始化控件无需在使用findViewById,直接把控件id当做控件的对象使用
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.longToast
import org.jetbrains.anko.toast

class MainActivity : AppCompatActivity() {

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

        //TODO 控件赋值
        tv_id.text = "Hello   Kotlin!!!!!!"

        //TODO 监听器  --- 修改按钮显示的文字
        but_id.setOnClickListener{but_id.text="你点击了以下按钮"}
        but_id.setOnClickListener{toast("你点击了按钮")}

        //TODO 长按监听器  +  toast
        but_long_id.setOnLongClickListener{longToast("小提示:  你长按的按钮!!!!!");true}
    }
}