詳解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)完成了,我們來寫第一個項目hello world
開始進(jìn)行第一個小Demo的使用
(1)在布局文件中寫一個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 CardView+ViewPager實現(xiàn)ViewPager翻頁動畫的方法
本篇文章主要介紹了Android CardView+ViewPager實現(xiàn)ViewPager翻頁動畫的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06Android 實現(xiàn)電話攔截及攔截提示音功能的開發(fā)
本文主要介紹Android 實現(xiàn)電話攔截和攔截提示音功能的開發(fā),這里提供實現(xiàn)代碼和詳細(xì)講解,有需要的小伙伴可以參考下2016-08-08android配合viewpager實現(xiàn)可滑動的標(biāo)簽欄示例分享
本文主要介紹了android實現(xiàn)可滑動的標(biāo)簽欄示例,配合viewpager作為標(biāo)簽欄,且可以設(shè)置每頁顯示的標(biāo)簽個數(shù),超出可滑動顯示,需要的朋友可以參考下2014-02-02Android webview 內(nèi)存泄露的解決方法
這篇文章主要介紹了Android webview 內(nèi)存泄露的解決方法的相關(guān)資料,需要的朋友可以參考下2017-07-07Android 中 Activity顯示隱式跳轉(zhuǎn)
這篇文章主要介紹了Android 中 Activity顯示隱式跳轉(zhuǎn)的實現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02Android對圖片Drawable實現(xiàn)變色示例代碼
這篇文章主要給大家介紹了關(guān)于Android對圖片Drawable實現(xiàn)變色的相關(guān)資料,文中通過示例代碼將實現(xiàn)的方法介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-08-08