美文网首页
Kotlin学习(一)

Kotlin学习(一)

作者: leonseven | 来源:发表于2017-05-24 14:41 被阅读49次

最近的I/O大会上kotlin被google定为官方语言,并在AS3.0上也已经搭载,笔者也搭上学习的车。

1.基本配置
打开Android studio, 在Plugins中的Browse Repositories中输入"kotlin",点击 Install, 没有翻墙下载会很慢并且会出现错误,多下几次就行了,下载完成后 在一个Activity界面 选择 工具栏的 code ——> Convert java file to kotlin file, 这时候你的Activity 代码就会变成

class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)
    }
}

这个之后再分析
现在 运行是会出错的, 要点击右上角的“Configure”进行配置

//配置过后 project的build.gradle

buildscript {
    ext.kotlin_version = '1.1.2-4'
    repositories {
        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.3'
        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
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
//配置过后 module的build.gradle

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

apply plugin: 'kotlin-android-extensions'//增加特性(剔除了findViewById())

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

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
}
repositories {
    mavenCentral()
}

现在基本配置完成,也可以运行了

2.控件操作
在module的build.gradle中增加
apply plugin: 'kotlin-android-extensions'//增加特性(剔除了findViewById())

然后在布局文件 这是两个TextView, id分别为 text1、text2
之后再activity中直接通过 id.text设置(比databinding还方便)

 text1.text = "Hello Kotlin!"
 text2.textSize = 18.0f

再在布局中写个Button,id为btn1

btn1.setOnClickListener {toast("Hello leon")} //点击弹出toast 

toast需要 新建一个 .kt文件, 写一个方法

fun Context.toast(msg:String){
    Toast.makeText(this, msg, Toast.LENGTH_SHORT).show()
}

这样只要一行代码 就实现了点击btn弹出toast
由此我们可以看出,kotlin中 函数的写法: 由关键字‘fun’声明
而 msg:String 表示String类型,与java写法相反并加了一个“:”

变量声明: var、 val
var 是可变的
val 不可变,常量

刚开始写kotlin代码,有很多错误,可以java文件里编写,在复制过来转换,慢慢的就记住了,这个要用到项目中还不太现实,还是有很多的坑,但终究是大势所趋,还是要好好学习的。

相关文章

网友评论

      本文标题:Kotlin学习(一)

      本文链接:https://www.haomeiwen.com/subject/rvbbxxtx.html