詳解Kotlin Android開發(fā)中的環(huán)境配置
詳解Kotlin Android開發(fā)中的環(huán)境配置
在Android Studio上面進(jìn)行安裝插件
在Settings ->Plugins ->Browse repositores.. ->kotlin 安裝完成后重啟Android Studio就生效了 如圖所示:

在Android Studio中做Kotlin相關(guān)配置
(1)在根目錄 的build.gradle中進(jìn)行配置使用,代碼如下:
buildscript {
ext.kotlin_version = '1.1.2-4'
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
(2)在app/build.gradle 中配置的使用
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
compile "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
repositories {
mavenCentral()
}
這樣,kotlin的配置就已經(jīng)完成了,我們來寫第一個(gè)項(xiàng)目hello world
開始進(jìn)行第一個(gè)小Demo的使用
(1)在布局文件中寫一個(gè)textview控件,代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.yoyoyt.kotlindemo.ThreeActivity">
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="aaaa"/>
</LinearLayout>
(2)我們進(jìn)行找id賦值使用
第一種找控件的方式 代碼如下:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView
import kotlinx.android.synthetic.main.activity_three.*
class ThreeActivity : AppCompatActivity() {
private var b : TextView ?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_three)
text.text="aaa"
}
}
第二找控件的方法 代碼如下:
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.TextView
import android.widget.Toast
class ThreeActivity : AppCompatActivity() {
private var b : TextView ?= null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_three)
b = findViewById(R.id.text) as TextView
b!!.text="shds"
Toast.makeText(this,b!!.text.toString(),Toast.LENGTH_SHORT).show()
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
純android代碼實(shí)現(xiàn)九宮格手勢密碼
這篇文章主要為大家詳細(xì)介紹了純android代碼實(shí)現(xiàn)九宮格手勢密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android模擬登錄評論CSDN實(shí)現(xiàn)代碼
本篇文章主要介紹了Android模擬登錄評論CSDN實(shí)現(xiàn)代碼,可以實(shí)現(xiàn)登陸發(fā)表評論到官方網(wǎng)站,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2016-11-11
Android實(shí)現(xiàn)多線程斷點(diǎn)下載的方法
這篇文章主要介紹了Android實(shí)現(xiàn)多線程斷點(diǎn)下載的方法,可實(shí)現(xiàn)開始、暫停下載及百分比進(jìn)度條等功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05
Android中Button實(shí)現(xiàn)點(diǎn)擊換圖案及顏色
大家好,本篇文章主要講的是Android中Button實(shí)現(xiàn)點(diǎn)擊換圖案及顏色,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下2022-01-01
Android進(jìn)程?;钪嵘M(jìn)程優(yōu)先級
這篇文章主要介紹了Android進(jìn)程?;钪嵘M(jìn)程優(yōu)先級,對提升優(yōu)先級感興趣的同學(xué)可以參考下2021-04-04
android 處理配置變更的實(shí)現(xiàn)方法
這篇文章主要介紹了android 處理配置變更的實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
Android 手機(jī)衛(wèi)士實(shí)現(xiàn)平移動畫示例
這篇文章主要介紹了Android 手機(jī)衛(wèi)士實(shí)現(xiàn)平移動畫的實(shí)例代碼,本文介紹的非常詳細(xì),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Android拼圖游戲 玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化
這篇文章主要介紹了Android拼圖游戲的實(shí)現(xiàn)方法,教大家玩轉(zhuǎn)從基礎(chǔ)到應(yīng)用手勢變化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Github簡單易用的?Android?ViewModel?Retrofit框架
這篇文章主要介紹了Github簡單易用的Android?ViewModel?Retrofit框架,RequestViewMode有自動對LiveData進(jìn)行緩存管理,每個(gè)retrofit api接口復(fù)用一個(gè)livedata的優(yōu)勢。下文具體詳情,感興趣的小伙伴可以參考一下2022-06-06

