Android中配置一个Kotlin开放环境

发布时间:2020-12-08 16:11:45 作者:Leah
来源:亿速云 阅读:108

本篇文章给大家分享的是有关Android中配置一个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控件,代码如下:

<&#63;xml version="1.0" encoding="utf-8"&#63;>
<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 &#63;= null
  override fun onCreate(savedInstanceState: Bundle&#63;) {
    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 &#63;= null
  override fun onCreate(savedInstanceState: Bundle&#63;) {
    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中配置一个Kotlin开放环境,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. 如何配置一个Android Studio环境
  2. Android Kotlin环境如何使用ButterKnife

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

kotlin android roi

上一篇:利用Java怎么对邮箱进行爬取

下一篇:利用怎么对Java输出打印工具类进行封装

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》