Kotlin第一天-------配置依賴及HelloWorld

聽說Kotlin代碼量可以減少好多,所以來學學。

一開始在網上各種看,然後又去Github和Kotlin官網看了一波,導入了各種依賴。結果發現AS自帶簡易化操作,簡單粗暴。

首先配置依賴:

AS直接導入:File--Setting--Plugins

搜索安裝Kotlin插件:

安裝成功後,重啓AS,重啓之後可以看見

這樣Kotlin就可以用了。

 

將老的項目轉換成Kotlin項目

Code-- Convert Java File to Kotlin File

然後會提示你當前工程未配置Kotlin,點一波就行,配置完成之後可以看到以下信息

這樣就完成了,開始在項目中用

xml文件:

<TextView
    android:id="@+id/textHello"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

然後在Activity中應用:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //查找控件
        val fistTv = findViewById<TextView>(R.id.textHello)
        //給控件設置值
        fistTv.text = "Hello World"

    }
}

學習代碼第一波Hello World 完成!!

 

發佈了58 篇原創文章 · 獲贊 6 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章