Kotlin 试用

在这里结合android studio使用android世界的swift语言,一种基于jvm的编程语言

1:新建一个project:如KotlinApp

2:android studio安装kotlin插件:kotlin与kotlin android extensions

3:在KotlinApp项目的Gradle文件中配置:

classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:0.11.91'

Kotlin 试用_第1张图片


4:应用kotlin插件:指定的moudle的gradle文件中添加:

apply plugin: 'kotlin-android'

Kotlin 试用_第2张图片


5:将指定的类转换成kotlin语言的语法类:

打开一个java类文件,点击工具栏Code->convert Java File to Kotlin File 

就可以将java转换成kotlin文件

如:

package com.xuan.kotlinapp

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
import android.view.View

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", Snackbar.LENGTH_LONG).setAction("Action", null).show() }
        //  TimeUnit.SECONDS;
    }

    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)
    }
}

普通的java bean:

package com.xuan.kotlinapp

/**
 * @author xuanyouwu
 * *
 * @email [email protected]
 * *
 * @time 2016-02-18 11:33
 */
class User {
    private val age: Int = 0
    private val name: String? = null
}


你可能感兴趣的:(Kotlin 试用)