当前位置: 移动技术网 > IT编程>移动开发>Android > 详解Kotlin Android开发中的环境配置

详解Kotlin Android开发中的环境配置

2019年07月24日  | 移动技术网IT编程  | 我要评论

中国母亲网,落魄党,丁丁地图无锡

详解kotlin android开发中的环境配置

在android studio上面进行安装插件

settings ->plugins ->browse repositores.. ->kotlin 安装完成后重启android studio就生效了 如图所示:

在android studio中做kotlin相关配置

(1)在根目录 的build.gradle中进行配置使用,代码如下:

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的配置就已经完成了,我们来写第一个项目hello world

开始进行第一个小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)我们进行找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()
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网