포스팅 OS : Mac

검색어 : 코틀린(Kotlin), 함수(Function), 변수(Variable), 에디트 텍스트(Edit Text)


▶︎ 이전 포스팅 목록

2018/02/10 - [Android_Kotlin] - [Android Kotlin] 안드로이드 스튜디오(Andorid Studio)에 코틀린(Kotlin) 개발환경 셋팅

2018/03/14 - [Android_Kotlin] - [Android Kotlin] 텍스트뷰(TextView)와 버튼(Button)을 사용한 헬로 코틀린



이번에는 코틀린의 함수와 변수에 대해 간단하게 알아보도록 할텐데 에디트텍스트에 입력한 두 개의 정수 중 큰 값을 출력하는 액티비티를 만들어 보겠습니다.


이번에도 역시 설명은 주석으로 보시면 되겠습니다. 사실 주석으로 설명을 달아 놓는 이유가 지금 포스팅 하는 소스들을 하나의 공부용 앱으로 만들어서 Git에 업로드 할 예정이라 소스만 보고도 이해가 가능해야 해서 이런 방식을 채택하게 되었습니다.



1. 레이아웃


<LinearLayout 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"
android:orientation="vertical"
tools:context="com.kotlin_test.Activity.FunctionActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="4">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="첫 번째 :"
android:gravity="center"/>
<EditText
android:id="@+id/edt_01"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="number"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="두 번째 :"
android:gravity="center"/>
<EditText
android:id="@+id/edt_02"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="number"/>

</LinearLayout>
<Button
android:id="@+id/btn_result1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Max 1"/>
<Button
android:id="@+id/btn_result2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Max 2"/>
<Button
android:id="@+id/btn_result3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Max 3"/>

<TextView
android:id="@+id/txt_result"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center"
android:textSize="20sp"/>

</LinearLayout>





2. 소스


import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.kotlin_test.R
import kotlinx.android.synthetic.main.activity_function.*

class FunctionActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_function)

// var : 변경 가능한 변수 선언
var maxNum : String

// MAX 1 버튼
btn_result1.setOnClickListener {

// val : 변경 불가능한 변수 선언
val num01 : Int = Integer.parseInt(edt_01.text.toString())

/*
타입 추론 기능에 의해 num01 변수와는 다르게 타입 지정을 생략할 수 있다. (: Int 생략 가능)
단, 변수 생성과 동시에 값을 초기화 해줄 경우에만 생략이 가능하다.
변수 초기화를 나중에 진행할 경우 아래와 같이 반드시 타입을 지정해 주어야 한다
val num02 : Int
*/
val num02 = Integer.parseInt(edt_02.text.toString())

maxNum = max1(num01, num02).toString()

txt_result.text = maxNum
}

// MAX 2 버튼
btn_result2.setOnClickListener {

val num01 : Int = Integer.parseInt(edt_01.text.toString())
val num02 = Integer.parseInt(edt_02.text.toString())

maxNum = max2(num01, num02).toString()

txt_result.text = maxNum
}

// MAX 3 버튼
btn_result3.setOnClickListener {

val num01 : Int = Integer.parseInt(edt_01.text.toString())
val num02 = Integer.parseInt(edt_02.text.toString())

maxNum = max3(num01, num02).toString()

txt_result.text = maxNum
}
}

// 블록이 본문인 함수
fun max1(a: Int, b : Int) : Int {
return if (a > b) a else b
}

// 식이 본문인 함수
fun max2(a : Int, b : Int) : Int = if (a > b) a else b

/*
식이 본문인 함수 간략화 (: Int 반환 타입 지정 생략)
식이 본문일 경우 굳이 사용자가 반환 타입을 적지 않아도 컴파일러가 함수 본문 식을 분석해서
식의 결과 타입을 함수 반환 타입으로 정해준다
이렇게 컴파일러가 프로그래머 대신 타입을 정해주는 기능을 <타입 추론>이라 부른다.
*/
fun max3(a: Int, b : Int) = if (a > b) a else b
}



3. 실행 결과





+ Recent posts