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

 更新时间:2017年06月23日 14:46:55   作者:拼搏的少年  
这篇文章主要介绍了详解Kotlin Android开发中的环境配置的相关资料,需要的朋友可以参考下

详解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()
  }
}

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

相关文章

  • Android N获取外置SD卡或挂载U盘路径的方法

    Android N获取外置SD卡或挂载U盘路径的方法

    今天小编就为大家分享一篇Android N获取外置SD卡或挂载U盘路径的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • 纯android代码实现九宫格手势密码

    纯android代码实现九宫格手势密码

    这篇文章主要为大家详细介绍了纯android代码实现九宫格手势密码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-07-07
  • Android模拟登录评论CSDN实现代码

    Android模拟登录评论CSDN实现代码

    本篇文章主要介绍了Android模拟登录评论CSDN实现代码,可以实现登陆发表评论到官方网站,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2016-11-11
  • Android实现多线程断点下载的方法

    Android实现多线程断点下载的方法

    这篇文章主要介绍了Android实现多线程断点下载的方法,可实现开始、暂停下载及百分比进度条等功能,非常具有实用价值,需要的朋友可以参考下
    2015-05-05
  • Android中Button实现点击换图案及颜色

    Android中Button实现点击换图案及颜色

    大家好,本篇文章主要讲的是Android中Button实现点击换图案及颜色,感兴趣的同学赶快来看一看吧,对你有帮助的话记得收藏一下
    2022-01-01
  • Android进程保活之提升进程优先级

    Android进程保活之提升进程优先级

    这篇文章主要介绍了Android进程保活之提升进程优先级,对提升优先级感兴趣的同学可以参考下
    2021-04-04
  • android 处理配置变更的实现方法

    android 处理配置变更的实现方法

    这篇文章主要介绍了android 处理配置变更的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • Android 手机卫士实现平移动画示例

    Android 手机卫士实现平移动画示例

    这篇文章主要介绍了Android 手机卫士实现平移动画的实例代码,本文介绍的非常详细,具有参考借鉴价值,需要的朋友可以参考下
    2016-10-10
  • Android拼图游戏 玩转从基础到应用手势变化

    Android拼图游戏 玩转从基础到应用手势变化

    这篇文章主要介绍了Android拼图游戏的实现方法,教大家玩转从基础到应用手势变化,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • Github简单易用的 Android ViewModel Retrofit框架

    Github简单易用的 Android ViewModel Retrofit框架

    这篇文章主要介绍了Github简单易用的Android ViewModel Retrofit框架,RequestViewMode有自动对LiveData进行缓存管理,每个retrofit api接口复用一个livedata的优势。下文具体详情,感兴趣的小伙伴可以参考一下
    2022-06-06

最新评论