Kotlin ,一種新的書寫android 的語言

轉載請註明出處

1, 什麼是kotlin

Kotlin是一門基於JVM的編程語言,它正成長爲Android開發中用於替代Java語言的繼承者。Java是世界上使用最多的編程語言之一,當其他編程語言爲更加便於開發者使用而不斷進化時,Java並沒有像預期那樣及時跟進。
使用Kotlin,你可以很容易的在Android工程中替代Java或者與Java混合使用。

2, AS 中kotlin 的配置

1,首先安裝kotlin插件

這裏寫圖片描述

2,項目的Project 的build.gradle中添加kotlin 的版本號

// Top-level build file where you can add configuration options common to all sub-projects/modules.
ext.kotlin_version = "1.0.3"
buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
        maven {
            url "https://jitpack.io"
        }
    }
}

在module中的build.gradle
再buildscript中

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

在dependencies中添加kotlin依賴

  //kotlin
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

然後添加對kotlin 的支持

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

3,使用kotlin

這裏寫圖片描述

3,Kotlin Demo

通過使用Kotlin+RecyclerView 來展示一組數據

首先創建基類BaseActivity


/**
 * Created by huanjulu on 16/8/19.
 */

abstract class BaseActivity : AppCompatActivity() {


    abstract val mContentLayoutResouredId: Int


    val toolbar: Toolbar by lazy { find<Toolbar>(R.id.toolbar) }//Kotlin 中的代理

    companion object { //這部分代碼你可以理解爲有點類似JAVA中的Static,是可以直接通過類名調用

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super<AppCompatActivity>.onCreate(savedInstanceState)

        if (mContentLayoutResouredId != 0) setContentView(mContentLayoutResouredId) else creatContentView()

        if (savedInstanceState != null) handleSavedInstanceState(savedInstanceState)

        if (intent.extras != null) handleIntentBundleExtrax(intent.extras)


        ButterKnife.bind(this)

        setSupportActionBar(toolbar)

        setUp()

    }

    override fun onResume() {
        super<AppCompatActivity>.onResume()
    }


    override fun onPause() {
        super<AppCompatActivity>.onPause()
    }


    override fun onDestroy() {
        super<AppCompatActivity>.onDestroy()
    }


    open fun creatContentView(): View {
        throw UnsupportedOperationException("sorry , your layoutviewId ==0 , createContentView() must be implemented by subclasS")
    }

    open fun handleIntentBundleExtrax(bundle: Bundle) {

    }

    open fun handleSavedInstanceState(savedInstanceState: Bundle?) {

    }

    open fun setUp() {

    }

    open fun handleIntentExtrax(key: Any): String? {
        if (key is String) return intent.getStringExtra(key)
        return null

    }
}




然後看下主類MainActivity :


/**
 * Created by huanjulu on 16/8/25.
 */

class MainActivity : BaseActivity() {
    override val mContentLayoutResouredId: Int = R.layout.activity_main

    val context = this

    val list = mutableListOf("Android", "Binder", "Asynctask", "OOM")
    override fun setUp() {
        super.setUp()

        var recyView = findViewById(R.id.id_recyclerview) as RecyclerView
        var mHotNewsDataAdapter = MainAdapter(LayoutInflater.from(this))
        recyView!!.layoutManager = LinearLayoutManager(context)
        recyView!!.adapter = mHotNewsDataAdapter

    }

}

說明:
1 :關於繼承

  • **在Kotin 中所有的類都有共同的父類 Any
  • **聲明一個明確的父類,需要在類頭後加冒號再加父類

2:關於屬性和變量

  • **在 Kotlin 中類可以有屬性,我們可以使用 var 關鍵字聲明可變屬性,或者用 val 關鍵字聲明只讀屬性(有點類似java中的final修飾符)
  • **通過名字直接使用一個屬性
    例如:val user=User()
    user.name
    user.age 等等

執行代碼
這裏寫圖片描述

更多的關於kotlin 的學習資料

https://github.com/outparadox/AndroidTechWiki

Demo 下載
https://github.com/outparadox/KotlinRecycle

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章