Kotlin學(xué)習(xí)一

今天主要講講 kotlin 的簡單配置,以及在配置中會遇到的問題

一 kotlin配置

最新的Androidstudio已經(jīng)集成了 kotlin,因此不需要做特別的配置,現(xiàn)在以新建一個kotlin項目做簡單講解

1.新建一個名為 KtDemo的項目,點擊next


1.png

2.直接點擊next


image.png

3.選擇 empty,繼續(xù)下一步
image.png

4.點擊finish


image.png

5,進入界面后是 Java 代碼,如下圖


image.png

6.將Java代碼轉(zhuǎn)成 kotlin,


image.png

7.此時界面代碼變成kotlin,但還需要對kotlin做些基本的配置,試著在代碼中敲一個回車鍵,會出現(xiàn)如下圖所示


image.png

8.點擊右上角 cinfigure,出現(xiàn)下圖所示,kotlin版本選擇最新版本,然后點擊ok


image.png

9,點擊ok后界面進入 app module 的 build.gradle 配置界面


image.png

10,在 apply plugin: 'kotlin-android' 下方導(dǎo)入新代碼

//kotlin擴展塊,方便初始化控件,類似butterknife的作用
apply plugin: 'kotlin-android-extensions'

導(dǎo)入后,顯示如下圖


image.png

此導(dǎo)入是為了方便控件初始化,類似butterknife一樣簡化初始控件代碼

11.再看下MainActivity的layout文件 activity_main.xml,很普通,代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.ktdemo.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="awke"/>

</android.support.constraint.ConstraintLayout>

12,在MainActivity中導(dǎo)入方便初始化控件的包:

import kotlinx.android.synthetic.main.activity_main.*

其中 activity_main 為當(dāng)前activity所加載的xml文件名,若有一個activity文件CodeActivity,其對應(yīng)的布局文件為 activity_code,則需要在 CodeActivity的頭部導(dǎo)入

import kotlinx.android.synthetic.main.activity_code.*

此處MainActivity的導(dǎo)入截圖如下:


image.png

別忘了要rebuild一下項目

13.然后寫一個簡單的按鈕點擊土司的功能,MainActivity代碼如下:

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

        btn2.setOnClickListener(this)

    }

    override fun onClick(v: View?) {
        when(v?.id){
          R.id.btn2 ->Toast.makeText(this,"wo",Toast.LENGTH_LONG).show()
        }

    }

14.下面說下“點擊右上角 cinfigure,出現(xiàn)下圖所示,kotlin版本選擇最新版本,”為什么要選擇比較高的版本,因為如果版本級別較低(如我當(dāng)時選擇的默認版本為 1.0.0),然后在編譯的時候出現(xiàn)以下error:

Error:(2, 0) Unable to find method 'org.gradle.api.internal.file.DefaultSourceDirectorySet.<init>(Ljava/lang/String;Lorg/gradle/api/internal/file/FileResolver;)V'.
Possible causes for this unexpected error include:<ul><li>Gradle's dependency cache may be corrupt (this sometimes occurs after a network connection timeout.)
Re-download dependencies and sync project (requires network)
The state of a Gradle build process (daemon) may be corrupt. Stopping all Gradle daemons may solve this problem.
Stop Gradle build processes (requires restart)
Your project may be using a third-party plugin which is not compatible with the other plugins in the project or the version of Gradle requested by the project.
In the case of corrupt Gradle processes, you can also try closing the IDE and then killing all Java processes.

我做了以下幾個處理:
1.重新下載,但是不行
2.重啟studio,還是不行
3.最后一條不是提示版本不匹配嗎?于是我下載了更新的 gradle,配置到項目中,但是還是報錯

后來發(fā)現(xiàn)原來是 kotlin 版本過低,kotlin版本顯示在 project的build.gradle中:


image.png

然后我將 kotlin版本改為1.1.51,重新編譯運行就ok了。

15。最后看看運行的結(jié)果:


12.gif

ok,今天就講到這里,謝謝

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容