Kotlin初体验

这两天看都在讨论Kotlin,也来试试,感觉不错,是更精简方便了。重要是可以用IDE,这两天看Python头疼,但它却自动转换!!冲这一点就牛B,更别说更方便响应式编写

给Android Studio安装Kotlin

File->Setitngs->Plugins->搜索Kotlin进行安装->重启Android Studio

新建Android Studio工程

将MainActivity转换为Kontlin语言

ctrl+shift+a->输入Convert Java F,选择Convert Java File to Kontlin File

ctrl+shift+a->输入configure k,选择Configure Kontlin in Project

这两条更新要一些时间,更新完之后就可以运行了

Code

  • MainActivity.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.xuie.kotlindemo

import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val toolbar = findViewById(R.id.toolbar) as Toolbar
setSupportActionBar(toolbar)

val fab = findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener { view -> Snackbar.make(view, "Replace with your own action, I'm kotlin", Snackbar.LENGTH_LONG).setAction("Action", null).show() }
}

override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
val id = item.itemId

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true
}

return super.onOptionsItemSelected(item)
}
}
  • app/build.gradle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 23
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.xuie.kotlindemo"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'
compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
}
buildscript {
ext.kotlin_version = '1.0.0'
repositories {
mavenCentral()
}
dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
repositories {
mavenCentral()
}

参考

https://kotlinlang.org/docs/tutorials/kotlin-android.html
http://blog.jetbrains.com/kotlin/2013/08/working-with-kotlin-in-android-studio/

文章作者: 二十I邊界
文章链接: https://xuie0000.com/post/2016-02-18-2019/Kotlin初体验.html
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 二十I邊界