In the previous article, we learned how to setup Kotlin in the android studio? Now moving to journey ahead we are going to develop our first app with Kotlin. It is the basic app, but it will let you know the structure of the program.
在上一篇文章中,我們學習了如何在android studio中設置Kotlin ? 現在,我們要前進,我們將與Kotlin開發我們的第一個應用程序 。 這是基本的應用程序,但是它將讓您知道程序的結構。
After creating a new Kotlin project, few files will be created in Java and layout folder. The java folder contains the kotlin (.kt) files and layout folder contains the .xml files. If you are still confused, just head over to the previous article and revise how to create a Kotlin project?
創建新的Kotlin項目后,將在Java和layout文件夾中創建很少的文件。 java文件夾包含kotlin( .kt )文件,而layout文件夾包含.xml文件。 如果您仍然感到困惑,請直接轉到上一篇文章,并修改如何創建Kotlin項目 ?
Let's have a look at those files:
讓我們看一下這些文件:
MainActivity.kt
MainActivity.kt
package com.onedreamers.myapplication
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
This is kotlin file which contains logics and functionality of our MainActivity.
這是kotlin文件,其中包含我們MainActivity的邏輯和功能。
activity_main.xml
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context="com.onedreamers.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
This is layout file which contains code for the design of our activity. Here we have a TextView that will show "Hello world" on the screen. This will remain same either we use java or kotlin as source language.
這是布局文件,其中包含用于設計我們的活動的代碼。 在這里,我們有一個TextView ,它將在屏幕上顯示“ Hello world” 。 我們將使用java或kotlin作為源語言,這將保持不變。
Build.gradle
Build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.onedreamers.myapplication"
minSdkVersion 21
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:support-v4:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}
Output
輸出量
After building the above project, just run it on emulator or any real device. You will see the following result.
構建完上述項目后,只需在模擬器或任何實際設備上運行它即可。 您將看到以下結果。
Conclusion:
結論:
So this article, was just to introduce you to the structure of a kotlin application. Any doubt? feel free to write it down. From the next article, we will know the actual magic of kotlin programming language and kotlin extension for android. You will be amazed of what kotlin can do - shorter code, type-safe and much more. Just stay connected. Good day.
因此,本文只是向您介紹kotlin應用程序的結構 。 任何疑問? 隨時寫下來。 從下一篇文章中,我們將了解kotlin編程語言和適用于android的kotlin擴展的真正魔力。 您將對kotlin可以做什么感到驚訝-較短的代碼,類型安全的等等。 保持聯系。 美好的一天。
翻譯自: https://www.includehelp.com/kotlin/develop-your-first-app-with-kotlin.aspx