Using Kotlin in Android Studio 3.0 (Part 1)

综合技术 DZone (源链)

Background

When I wrote this article, I assumed that you already know about Android programming before. If you are Android beginners, you can read my other article, “ Introduction Android programming using Android Studio 3.0 .”

Why Is Kotlin?

  • Kotlin is very intuitive and easy to learn for Java developers
  • Android Studio 3.0 can understand, compile, and run Kotlin code easily

Installing the Android Studio 3.0 and Kotlin Plugin

  • We must download and install Android Studio 3.0 at developer.android.com (current version is 3.0.1)
  • Checking Kotlin plugin installed by selecting File > Settings

Creating a New Android Application Using Kotlin

  • Creating a new project by selecting File > New > New Project . Enter a text for Application name and check out Include Kotlin Support checkbox as the following screenshot:

  • Click Next and Next and select Empty Activity . Click Next and Finish .
  • So far, we have activity_main.xml file (includes two modes: Design and Text ) and MainActivity.kt file.
  • In right window, select app > res > values > strings.xml . Add a string element name hello_world to strings.xml file as follows:
    My First Kotlin App
    Hello Kotlin
  • Select activity_main.xml file in Design mode. Select TextView and set a value ( message ) for ID and set a value (@string/hello_world) for text in Attributes window:

  • Select MainActivity.kt file and adding the import as follows:
import kotlinx.android.synthetic.main.activity_main.*
  • Add the following code line to the onCreate method:
message.text = "Hello My Kotlin App in Android Studio 3.0"
  • Do you remember the message ? It is ID of TextView .
  • The complete code of MainActivity.kt , so far, can look like:
package com.example.admin.myfirstkotlinapp    
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import kotlinx.android.synthetic.main.activity_main.*  
class MainActivity : AppCompatActivity() {        
override fun onCreate(savedInstanceState: Bundle?) {          
     super.onCreate(savedInstanceState)          
     setContentView(R.layout.activity_main)          
     message.text = "Hello My Kotlin App in Android Studio 3.0"      
     }  }
  • Run App and the message ” Hello My Kotlin App in Android Studio 3.0 ” will display.

Conclusion

In this article, I have only introduced how to install and create a simple Android application using Kotlin in Android Studio 3.0. In my next articles, I will introduce about more Kotlin language and how to create more complicate Android applications.

您可能感兴趣的

Android JNI之执杖生涯 .a -> .so(2)(android studio)... 连载 又见一帘幽梦,jni .a的故事还在继续。原本打算跟mk方式 一起,名字都想好了,大道三千,我只取二瓢。燃,故事开始。 gradle-experimental 故事的猪脚。一个插件,专门为 JNI 服务。E文好的同学,大体也猜出来了,这个是谷狗实验室里边的玩意。东西,是...
使用Kotlin构建MVVM应用程序—第四部分:依赖注入Dagger2... 写在前面 这里是使用Kotlin构建MVVM应用程序—第四部分:依赖注入Dagger2 在前面的一系列文章中,我们了解了在MVVM架构中是如何提供和处理数据的。 //////model val remote=Retrofit.Builder() .base...
Functional operations with collections in Kotlin (... I must admit that, for me, one of the most frustrating things when writing Java ccode is the list handling. Java 8 has some improvements in this res...
Building Libraries for Kotlin Kotlin has a lot of great features that improve developer productivity and make the overall code base more stable. This all works great when your enti...
详细讲解Android Studio中使用Git——结合GitLab... 1. 前言 以前项目版本管理工具一直用的都是SVN,现在换成Git,并且项目托管平台使用的是GitLab。虽然经常用GitHub,但是却很少使用到分支,因为都是自己在写,也不用开发版与发布版、打Tag那些,所以这里也详细讲下Git的使用。这篇文章还是会按我以前的写作风格,写的通俗易懂,并且...
DZone责编内容来自:DZone (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » Using Kotlin in Android Studio 3.0 (Part 1)



专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录