用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 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.

構建完上述項目后,只需在模擬器或任何實際設備上運行它即可。 您將看到以下結果。

first App with Kotlin

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/379432.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/379432.shtml
英文地址,請注明出處:http://en.pswp.cn/news/379432.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

數據結構與算法分析-第一章Java類(02)

編寫一個名為Person的類&#xff0c;它包含分別表示人的名字與年齡的兩個數據域。要求此類包含對其中任何一個數據域進行設置與獲取的方法。還要求包含可進行下列測試的方法&#xff1a; 兩個Person對象是否相等--即是否有相同的名稱與年齡一個人是否比另一個人年長 最后&#…

asp.net對于長篇文章進行分頁

對于文章篇幅比較長的&#xff0c;就必須采用分頁顯示。在.net中對長篇文章分頁一般有2種方法&#xff0c;第一種就是先計算好一頁的文字長度是多少&#xff0c;然后把文章總的長度除設置好的單頁文字長度及可&#xff0c;用這方法可以減少認為進行分頁的繁瑣&#xff0c;但是這…

匯編語言-004(LABEL 、間接尋址、變址操作數、指針使用、TypeDef、LOOP、DWORD變量交換高位低位字)

1&#xff1a; LABEL : 為一個標號定義大小屬性&#xff0c;但不分配內存與下一個變量共用內存&#xff0c;與C中UNION類似 .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCoed:DWORD.data val16 LABEL WORD val32 DWORD 12345678hLongValue LABEL DWORD val1…

(只需挨個復制粘貼命令即可部署)在Centos7下搭建文件服務器(VSFTPD)

觀看北京尚學堂-百戰程序員筆記一、VSFTPD簡介 Linux的組件&#xff08;一款軟件&#xff09;&#xff0c;安裝到Linux后可以通過java代碼&#xff08;FtpClient&#xff09;實現文件的上傳。基于FTP協議。 由于VSFTPD是基于FTP協議&#xff0c;客戶端瀏覽器是需要通過http協議…

POJ 2421 Constructing Roads MST kruskal

最近剛學的并查集所以用kruskal來試試最小生成樹~ kruskal其實用幾句話就能說完~ 1.貪心所有邊的權值,從小到大取值 2.取值時~將邊權非0的兩個頂點~進行并查操作~如果兩個點的祖先不同...邊權加入最小生成樹...并且將兩個點納入同一個集合中 3.判斷是否所有點都在同一個集合中…

c# 聲明類的時候初始化類_使用C#初始化的列表聲明

c# 聲明類的時候初始化類The task is to create/declare a list with an initializer list in C#. 任務是在C&#xff03;中使用初始化列表創建/聲明一個列表 。 C&#xff03;清單 (C# List) A list is used to represent the list of the objects, it is represented as Lis…

編寫程序計算所輸日期是當年的第幾天

/* 1.輸入年月日&#xff0c;編寫程序計算所輸日期是當年的第幾天 *//* 2.已知列車隔日發車&#xff0c;且1/1/2006不發車(無ticket),如果所輸入數據在此日期之后&#xff0c;則輸出有沒有車票&#xff0c;否則僅輸出上一步結果。*/ /* month/date/year is which day of the ye…

匯編語言-005(XCHG、標志位操作、算術操作、比例因子的變址尋址、多個常用運算符運用、大端轉小端、數組操作)

1: 用不超過3條XCHG指令對4個8位寄存器的值重新排序&#xff0c;A,B,C,D調整為D,C,B,A .386 .model flat,stdcall.stack 4096 ExitProcess PROTO,dwExitCode:DWORD.data.code main PROCmov al,Amov bl,Bmov cl,Cmov dl,Dxchg al,dlxchg bl,clINVOKE ExitProcess,0 main ENDP E…

bcd碼二進制轉十進制_二進制編碼的十進制(BCD碼)及其加法

bcd碼二進制轉十進制Prerequisite: Number systems 先決條件&#xff1a; 數字系統 BCD Code (8421 Code): In BCD 8421 code, each decimal digit is represented using a 4-bit binary number. The 4-bit binary numbers have their weights attached as 8, 4, 2, 1 from MS…

SVN服務器部署

一、SVN版本控制器 Subversion就是一款實現版本控制的工具軟件&#xff0c;通常也成為版本控制器&#xff0c;簡稱SVN。 Subversion是Apache軟件基金會組織下的一個項目 SVN基本操作&#xff1a; checkout&#xff08;檢出&#xff09;&#xff1a;將一個服務端創建好的項目…

rtmp流\http流測試地址

測試方式&#xff1a;ffplay rtmp://58.200.131.2:1935/livetv/cctv1 rtmp&#xff1a; CCTV-1綜合:rtmp://58.200.131.2:1935/livetv/cctv1 CCTV-2財經:rtmp://58.200.131.2:1935/livetv/cctv2 CCTV-3綜藝:rtmp://58.200.131.2:1935/livetv/cctv3 CCTV-4中文國際:rtmp://58.2…

LINQ to XML:如何讀寫XCData

using System;using System.Xml.Linq;namespace ConsoleApplication1 {class Program{static void Main(string[] args){//寫入CDATA元素塊var doc new XElement("Test",new XElement("User",new XAttribute("name", "chenxizhang"),…

C#中的結構和類之間的區別

C&#xff03;類和結構 (C# class and structure) In C# and other programming languages, structure and classes are used to define a custom data type, that we can organize according to our need with different types of variables, methods etc. 在C&#xff03;和其…

[轉載]SQL?Plus?一些使用技巧

原文地址&#xff1a;SQL Plus 一些使用技巧作者&#xff1a;☆水『若寒Sql*plus的使用 Sql*plus介紹 Sql*plus是oracle提供的一個工具程序&#xff0c;既可以在oracle服務器使用&#xff0c;也可以在oracle客戶端使用。在windows下分兩種&#xff0c;sqlplus.exe是命令行程序&…

云服務器(Centos)部署SVN

1&#xff0c;安裝svn yum install subversion 2&#xff0c;查看版本號 svnserve --version 3&#xff0c;創建SVN版本庫&#xff08;在var/svn 文件夾下&#xff09; 新建文件夾 mkdir -p /var/svn/svnrepos 創建版本庫 svnadmin create /var/svn/svnrepos 4&#xff0c;修改…

ffmpeg命令提取像素格式

1&#xff1a; 提取yuv格式&#xff1a;不修改寬高 取3秒 ffmpeg -i test_1920x1080.mp4 -t 3 yuv420p_orig.yuv ffmpeg -i test_1920x1080.mp4 -t 3 -pix_fmt yuv420p yuv420p_orig.yuv 可以使用ffplay播放&#xff1a;ffplay -video_size 1920x1080 yuv420p_orig.yuv 提取y…

Javascript(js)使用function定義構造函數

Javascript并不像Java、C#等語言那樣支持真正的類。但是在js中可以定義偽類。做到這一點的工具就是構造函數和原型對象。首先介紹js中的構造函數。 Javascript中創建對象的語法是在new運算符的后面跟著一個函數的調用。如 1 varobj newObject();2 vardate newDate();運算符new首…

錯誤:將字符串分配給C中的char變量| 常見的C程序錯誤

If you assign a string to the character variable, it may cause a warning or error (in some of the compilers) or segmentation fault error occurs. 如果將字符串分配給字符變量&#xff0c;則可能會導致警告或錯誤(在某些編譯器中)或發生分段錯誤。 Consider the code…

【轉】用BibTeX 寫 Reference

BibTeX 是一種格式和一個程序&#xff0c; 用于協調LaTeX的參考文獻處理&#xff0c;BibTeX 使用數據庫的的方式來管理參考文獻.&#xff0c;BibTeX 文件的后綴名為 .bib。 例子&#xff1a; article{name1, author {作者, 多個作者用 and 連接}, title {標題}, journal {期…

計算機二級C語言易混淆的區別

1&#xff0c;if(a1)與if(a1)的區別 首先&#xff0c;if(a1) 等價于 a1;if(a); 而a 1&#xff0c;是判斷a是不是為1&#xff1b; if(sq)里面的分為兩種情況&#xff0c;一種是sq為0&#xff0c;不執行if里面的代碼內容&#xff1b;另一種是sq不為0&#xff0c;執行里面的代碼內…