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作为源语言,这将保持不变。

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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

用Kotlin开发您的第一个应用程序| Android与Kotlin相关推荐

  1. 使用VS Code开发AngularJS 2 第一个应用程序

    使用VS Code开发AngularJS 2 第一个应用程序 目录 运行环境 创建项目 安装依赖包 创建TypeScript应用程序 配置应用程序 运行应用程序 运行环境 运行环境: Windows ...

  2. 使用Eclipse开发Spring的第一个简单程序

    使用Eclipse开发Spring的第一个简单程序 本篇文章将通过一个简单的入门程序向读者演示Spring框架的使用过程,具体如下: 使用Eclipse创建Web应用并导入JAR包 使用Eclipse ...

  3. 用kotlin方式打开《第一行代码:Android》之开发酷欧天气(2)

    参考:<第一行代码:Android>第2版--郭霖 注1:本文为原创,例子可参考郭前辈著作:<第一行代码:Android>第2版 注2:本文不赘述android开发的基本理论, ...

  4. 用kotlin方式打开《第一行代码:Android》之开发酷欧天气(1)

    参考:<第一行代码:Android>第2版--郭霖 注1:本文为原创,例子可参考郭前辈著作:<第一行代码:Android>第2版,转载请注明出处! 注2:本文不赘述androi ...

  5. 初学编程 第一个小程序Android studio实现计算器功能

    源代码下载:https://gitee.com/zha-yingying/calculator.git 1.建立一个新的Layout,我这里使用的是GridLayout(网格布局),提取屏幕宽度(方便 ...

  6. [Android]使用Kotlin开发Android(二)

    以下内容为原创,欢迎转载,转载请注明 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/4829007.html [TOC] 使用Kotlin+OkHtt ...

  7. 安卓重构系列-01使用Kotlin开发第一个Demo

    第一个Kotlin安卓项目 背景 首先,你必须熟练掌握Kotlin这门编程语言,上手难度是不高的,而且真心很好用. 如今,越来越多的安卓项目使用Kotlin开发,现在已经有30%的项目使用Kotlin ...

  8. Kotlin 视频课程系列一:《零基础学习 Kotlin 编程》

    Kotlin 系列课程一:<零基础学习 Kotlin 编程> 课程简介 在你决定要打开这个页面阅读的瞬间,风云巨变互联网行业里拼搏的你, 已经得到了一个引领时代 , 改变命运,突破瓶颈的机 ...

  9. 超越Android:Kotlin在后端的工作方式

    by Adam Arold 亚当·阿罗德(Adam Arold) 超越Android:Kotlin在后端的工作方式 (Going Beyond Android: how Kotlin works on ...

最新文章

  1. 伟大前程与技术难关:量子机器学习该如何走进现实?
  2. 空气动力学测试软件,山寨也精确 简单方法测试最佳空气动力学姿势(图文)
  3. CVPR 2018 | TVNet:可端到端学习视频的运动表征
  4. easymock教程_EasyMock教程–入门
  5. 简单好用的计算器:bc
  6. PyCharm运行出现 Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run
  7. 高级语言程序设计II 实验报告三c++使用文本文件和二进制文件的读写比较两者的区别和优劣...
  8. 英雄启动出错解决方法_超纯水设备高压泵不启动解决方法
  9. 可怕!离职都开始内卷了?!
  10. ajax前面$,连续多次发送Ajax前面的请求被后面的覆盖以致不能执行的问题
  11. 【系统分析师之路】如何备考系统分析师与架构师(软件水平考试)
  12. office增加自定义文档模板
  13. airpin linux电脑,AirPinPcSender
  14. TestStand-调试VI
  15. oracle 中 使用 begin end 批量更新,删除资料
  16. 政务内网、政务外网、政务专网
  17. cs1.6一直连接服务器,CS1.6连接不上服务器解决办法
  18. Matlab syms 矩阵变量,matlab syms.m
  19. 如何在windows 10或11下制作外接便携式可移植启动盘(以安装最新的ubuntu 22.04为例)
  20. 192、如何查询局域网内在线的监控设备IP

热门文章

  1. 初中学习计算机基础,初中生计算机基础学习心得
  2. Web安全相关(三):开放重定向(Open Redirection)
  3. css图片的全屏显示代码-css3
  4. Json Schema的使用
  5. 正确判断js数据类型 总结记录
  6. Flexible 弹性盒子模型之flex
  7. 一招搞定css页面布局
  8. 开发辅助 | 阿里图标库iconfont入门使用
  9. 从零开始的全栈工程师——html篇1
  10. 基于 vue 的验证码组件