[Android] Anonymous Function + Lambda (feat. Click Listenenr)

2020. 5. 4. 22:27·Android/OS

 

 

흔히 Android에서

Button.SetOnClickListener 를 자주 사용하는데

 

 

이때 꼭 알아야할 개념이

Lambda function 과 Listener이다.

 

 

Listener의 동작 원리?

Android OS는 View에 달려있는 모든 Listener를 켜놓는다. 

View.OnClickListener에는 onClick이라는 메소드가 존재하고

실제 클릭 이벤트가 발생할 때마다 어느 View에서 이벤트가 발생했는지 식별하여 

오버라이딩한 메소드 동작을 수행한다.

 

동작 수행의 주체는 Android OS이다.

 

 


예제를 통해 아라보자

 

[Layout.xml]
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">

    <Button
        android:id="@+id/lambda_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="람다함수 버튼"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintRight_toLeftOf="@id/anonymous_button"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <Button
        android:id="@+id/anonymous_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="익명함수 버튼"
        app:layout_constraintLeft_toRightOf="@id/lambda_button"
        app:layout_constraintRight_toLeftOf="@id/declaration_button"
        app:layout_constraintBottom_toBottomOf="@id/lambda_button"
        app:layout_constraintTop_toTopOf="@id/lambda_button"/>
    <Button
        android:id="@+id/declaration_button"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="선언함수 버튼"
        app:layout_constraintLeft_toRightOf="@id/anonymous_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="@id/lambda_button"
        app:layout_constraintBottom_toBottomOf="@id/lambda_button" />


</androidx.constraintlayout.widget.ConstraintLayout>

 

[Activity.kt]
package com.example.android_tutorial

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.function_test_layout.*

class FunctionTestActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.function_test_layout)
        init()
    }




    private fun init() {
        
//        람다식으로 인자 전달
        lambda_button.setOnClickListener {
            Log.i("Function Test", "this is Lambda Button click")
        }

//        object 익명함수 객체로 인자 전달
        anonymous_button.setOnClickListener(object : View.OnClickListener {
            override fun onClick(v: View?) {
                Log.i("Function Test", "this is anonymous function Button click")
            }
        })

//    함수 선언
        val clickListenFunction = object : View.OnClickListener {
            override fun onClick(v: View?) {
                Log.i("Function Test", "this is declarative function Button click")
            }
        }

//        선언함수를 리스너의 인자로 넘김
        declaration_button.setOnClickListener(clickListenFunction)
    }
    
}

 

 

[결과화면]

각 버튼을 누를 때마다 로그가 기록된다.
세 버튼 모두 같은 동작을 수행한다.


위의 예제에서 알 수 있듯이

어떤 방식으로 구현하든 같은 동작을 수행하고 또 의미도 같지만

최신 Android Studio 에서는 람다식의 사용을 권장한다.

 

 

[Listener]

 

OnClickListener 객체의 원형을 살펴보자.

위에 보이는 것과 같이 인터페이스 클래스이기 때문에

onClick이라는 함수를 따로 구현(Overriding) 해줘야한다.

 

그래서 익명객체인 

object: View.onClickListener {

override fun onClick(v: View?) {

~~~ }

}

와 같은 형태가 나오는 것이다.

 

람다식은 대체 파라미터인 View를 어떻게 인자로 전달하나?

 

Android Studio로 람다식을 사용하다보면 다음과 같은 

'it' 키워드를 만나게된다.

it 의 type이 View! (non-null)이다.

따로 명세하지 않아도 View 객체를 파라미터로 넘긴다고 암묵적으로 약속되어있다.

 

그야말로 개꿀딱이다.

 

저작자표시 (새창열림)

'Android > OS' 카테고리의 다른 글

[Android] Activity Life Cycle  (0) 2020.05.01
[Android] Constraint Layout  (0) 2020.04.14
[Android] Tab - Layout  (0) 2020.04.13
'Android/OS' 카테고리의 다른 글
  • [Android] Activity Life Cycle
  • [Android] Constraint Layout
  • [Android] Tab - Layout
M_Falcon
M_Falcon
  • M_Falcon
    Falcon
    M_Falcon
  • 전체
    오늘
    어제
    • 분류 전체보기 (432)
      • Web (16)
        • Nodejs (14)
        • Javascript (23)
        • FrontEnd (4)
      • DataBase (39)
        • Fundamental (1)
        • Redis (4)
        • PostgreSQL (10)
        • NoSQL (4)
        • MySQL (9)
        • MSSQL (3)
        • Error (4)
      • Algorithm (79)
        • Algorithm (문제풀이) (56)
        • Algorithm (이론) (23)
      • JVM (65)
        • Spring (13)
        • JPA (5)
        • Kotlin (13)
        • Java (24)
        • Error (7)
      • 기타 (70)
        • Kafka (3)
        • Kubernetes (3)
        • Docker (13)
        • git (19)
        • 잡동사니 (27)
      • 재테크 (11)
        • 세무 (4)
        • 투자 (3)
        • 보험 (0)
      • BlockChain (2)
        • BitCoin (0)
      • C (32)
        • C (10)
        • C++ (17)
        • Error (3)
      • Low Level (8)
        • OS (3)
        • 시스템 보안 (5)
      • 네트워크 (3)
      • LINUX (30)
        • Linux (26)
        • Error (4)
      • 저작권과 스마트폰의 이해 (0)
      • 생각 뭉치 (6)
      • 궁금증 (2)
      • Private (4)
        • 이직 경험 (0)
        • 꿈을 찾아서 (1)
      • Android (21)
        • OS (4)
  • 블로그 메뉴

    • 홈
    • WEB
    • 알고리즘
    • DataBase
    • Linux
    • Mobile
    • C
    • 방명록
  • 링크

    • github
  • 공지사항

  • 인기 글

  • 태그

    백준
    C++
    linux
    java
    database
    프로그래머스
    android
    Programmers
    Spring
    Kotlin
    JPA
    PostgreSQL
    알고리즘
    kafka
    docker
    algorithm
    Bitcoin
    ubuntu
    Git
    javascript
  • hELLO· Designed By정상우.v4.10.3
M_Falcon
[Android] Anonymous Function + Lambda (feat. Click Listenenr)
상단으로

티스토리툴바