100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 用Kotlin开发您的第一个应用程序| Android与Kotlin

用Kotlin开发您的第一个应用程序| Android与Kotlin

时间:2024-03-04 19:09:52

相关推荐

用Kotlin开发您的第一个应用程序| Android与Kotlin

In the previous article, we learned how to setup Kotlin in the android studio? Now moving to journey ahead we are going todevelop 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.myapplicationimport android.support.v7.app.AppCompatActivityimport android.os.Bundleclass 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 ourMainActivity.

这是kotlin文件,其中包含我们MainActivity的逻辑和功能。

activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="/apk/res/android"xmlns:app="/apk/res-auto"xmlns:tools="/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="com.onedreamers.myapplication.MainActivity"><TextViewandroid: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 26defaultConfig {applicationId "com.onedreamers.myapplication"minSdkVersion 21targetSdkVersion 26versionCode 1versionName "1.0"testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"}buildTypes {release {minifyEnabled falseproguardFiles 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 tointroduce 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可以做什么感到惊讶-较短的代码,类型安全的等等。 保持联系。 美好的一天。

翻译自: /kotlin/develop-your-first-app-with-kotlin.aspx

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。