๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ’ป Extracurricular/์ฝ”๋ฎค๋‹ˆํ‹ฐ ๋ชจ๊ฐ์ฝ”

๋ชจ๊ฐ์ฝ” < Kotlin ๊ธฐ์ดˆํŽธ > - 7์ผ์ฐจ

โœ… ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ๋Š”์ง€ ๊ฐ์ง€ํ•˜๊ธฐ - ๋กœ๊ทธ๋กœ ํ™•์ธํ•˜๊ธฐ

๋ฒ„ํŠผ์˜ id ์„ค์ • id๋Š” ํ”„๋กœ์ ํŠธ ์•ˆ์—์„œ ๊ณ ์œ (unique)ํ•ด์•ผ ํ•จ!

๋ฒ„ํŠผ์˜ id๊ฐ€ runButton ์ด๋ผ๊ณ  ๊ฐ€์ •

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
				findViewById<FloatingActionButton>(R.id.runButton).setOnClickListener {
            Log.d("๋กœ๋˜์•ฑ", "๋ฒ„ํŠผ ๋ˆ„๋ฆ„")
        }
    }
}

์œ„์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•˜๊ธฐ ์œ„ํ•ด ์ด 4๊ฐ€์ง€ import๊ฐ€ ํ•„์š”ํ•จ!

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.google.android.material.floatingactionbutton.FloatingActionButton

์•ˆ๋“œ๋กœ์ด๋“œ ์ŠคํŠœ๋””์˜ค์—์„œ๋Š” ์ž๋™์œผ๋กœ import ํ•ด์ฃผ์ง€๋งŒ,

์ž๋™์œผ๋กœ ์ธ์‹์ด ๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด?

  1. ์ง์ ‘ import ์ฝ”๋“œ ์ž‘์„ฑ
  2. alt + enter ๋ฅผ ๋ˆŒ๋Ÿฌ importํ•˜๊ธฐ

 

๐Ÿ‘€ ์ฝ”๋“œ ํ•ด์„

findViewById<FloatingActionButton>(R.id.runButton).setOnClickListener {
    Log.d("๋กœ๋˜์•ฑ", "๋ฒ„ํŠผ ๋ˆ„๋ฆ„")
}

1๏ธโƒฃ findViewById(R.id.runButton)

: ํ”„๋กœ์ ํŠธ ์•ˆ์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ ์ค‘์—์„œ id๊ฐ€ runButton์ธ FloatingActionButton ์„ ์ฐพ๋Š”๋‹ค.

 

2๏ธโƒฃ ~~~.setOnClickListener { ….. }

: ‘.’ ์•ž์— ์žˆ๋Š” ์ปดํฌ๋„ŒํŠธ๊ฐ€ ํด๋ฆญ ๋˜์—ˆ์„ ๋•Œ ์ค‘๊ด„ํ˜ธ({}) ์•ˆ์˜ ์ฝ”๋“œ๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

3๏ธโƒฃ Log.d(“๋กœ๋˜์•ฑ”, “๋ฒ„ํŠผ ๋ˆ„๋ฆ„”)

: ๋กœ๋˜์•ฑ์ด๋ผ๋Š” ํƒœ๊ทธ๋กœ ๋ฒ„ํŠผ ๋ˆ„๋ฆ„์„ Logcat์— ์ถœ๋ ฅํ•œ๋‹ค.

 

๋”ฐ๋ผ์„œ ๋นŒ๋“œ ํ›„ runButton ํด๋ฆญํ•˜๋ฉด

-> Logcat์— “๋ฒ„ํŠผ ๋ˆ„๋ฆ„” ์ด๋ผ๋Š” ๋กœ๊ทธ๊ฐ€ ์ถœ๋ ฅ๋จ

Logcat์˜ ๊ฒ€์ƒ‰์ฐฝ์— ๋กœ๋˜์•ฑ ์ด๋ผ๋Š” ํ…์ŠคํŠธ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋ฉด ๋กœ๋˜์•ฑ์ด ํƒœ๊ทธ์ธ ๋กœ๊ทธ๋งŒ ํ™•์ธ ๊ฐ€๋Šฅ


โœ… ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ๋Š”์ง€ ๊ฐ์ง€ํ•˜๊ธฐ - ํ…์ŠคํŠธ ๋ฐ”๊พธ๊ธฐ

๐Ÿ‘€ ๊ฐ„๋‹จํ•˜๊ฒŒ ๋ฒ„ํŠผ์„ ๋ˆŒ๋ €์„ ๋•Œ ํ…์ŠคํŠธ๋ฅผ ๋ณ€๊ฒฝ

findViewById<FloatingActionButton>(R.id.runButton).setOnClickListener {
    findViewById<Button>(R.id.lottoNum1).text = "45"
}

Log.d(“๋กœ๋˜์•ฑ”, “๋ฒ„ํŠผ ๋ˆ„๋ฆ„”)์ด๋ผ๋Š” ์ฝ”๋“œ ๋Œ€์‹ 

findViewById(R.id.lottoNum1).text = “45” ์ฝ”๋“œ ์ถ”๊ฐ€
  • id๊ฐ€ lottoNum1์ธ Button ์ปดํฌ๋„ŒํŠธ์˜ text ์†์„ฑ์„ “45”๋กœ ์ง€์ •ํ•˜๋ผ๋Š” ๋œป
  • ์ด๋•Œ text๋Š” ์ˆซ์ž๊ฐ€ ์•„๋‹Œ ๋ฌธ์ž or ๋ฌธ์žฅ์ด์–ด์•ผ ํ•จ (๋ฌธ์žฅ์€ “” ๋กœ ํ‘œํ˜„)

โœ… ๋ฒ„ํŠผ์ด ๋ˆŒ๋ ธ๋Š”์ง€ ๊ฐ์ง€ํ•˜๊ธฐ - ๋ฐฐ๊ฒฝ์ƒ‰ ๋ฐ”๊พธ๊ธฐ

๐Ÿ‘€ ๋ฐฐ๊ฒฝ์ƒ‰์„ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ ์ถ”๊ฐ€

findViewById<FloatingActionButton>(R.id.runButton).setOnClickListener {
    findViewById<Button>(R.id.lottoNum1).text = "45"
    findViewById<Button>(R.id.lottoNum1).backgroundTintList = ColorStateList.valueOf(Color.rgb(255,0,0))
}

setOnClickListener ์ค‘๊ด„ํ˜ธ ์•ˆ์— ์›ํ•˜๋Š” ์ฝ”๋“œ ์ถ”๊ฐ€

 

๐Ÿ‘€ ๋ฐฐ๊ฒฝ์ƒ‰์„ ๋ฐ”๊พธ๋Š” ์ฝ”๋“œ

findViewById<Button>(R.id.lottoNum1).backgroundTintList = ColorStateList.valueOf(Color.rgb(255,0,0))

backgroundTintList ์†์„ฑ์„ ColorStateList.valueOf(Color.rgb(r๊ฐ’, g๊ฐ’, b๊ฐ’)) ์œผ๋กœ ์„ค์ •

-> ์›ํ•˜๋Š” r, g, b ์ปฌ๋Ÿฌ๋กœ ๋ฐฐ๊ฒฝ์ƒ‰ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ


โœ… ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜๊ธฐ

๋ณ€์ˆ˜๋ž€?

  • ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•ด๋†“๋Š” ๊ณต๊ฐ„
  • ์ˆซ์ž, ๋ฌธ์ž, ์ปดํฌ๋„ŒํŠธ ๋“ฑ์„ ์ €์žฅ, ์‚ฌ์šฉ ๊ฐ€๋Šฅ
  • ์ค‘๊ฐ„์— ๋‹ค๋ฅธ ๋ฐ์ดํ„ฐ๋กœ ๋ฎ์–ด์”Œ์›Œ ์ €์žฅ ๊ฐ€๋Šฅ

์ฝ”ํ‹€๋ฆฐ์—์„œ๋Š” ๋ณดํ†ต ์ปดํฌ๋„ŒํŠธ๋ฅผ ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•ด๋‘๊ณ  ์‚ฌ์šฉํ•จ.

val num1 = findViewById<Button>(R.id.lottoNum1)
findViewById<FloatingActionButton>(R.id.runButton).setOnClickListener {
    num1.text = "45"
    num1.backgroundTintList = ColorStateList.valueOf(Color.rgb(255,0,0))
}
findViewById<FloatingActionButton>(R.id.runButton).setOnClickListener {
    findViewById<Button>(R.id.lottoNum1).text = "45"
    findViewById<Button>(R.id.lottoNum1).backgroundTintList = ColorStateList.valueOf(Color.rgb(255,0,0))
}

์œ„์™€ ์•„๋ž˜์˜ ์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ๊ฐ™์Œ!

 

๋‹ค๋ฅธ ์ ์€?

์œ„์˜ ์ฝ”๋“œ์—์„œ๋Š” **findViewById(R.id.lottoNum1)**๋กœ ์ฐพ์€ ์ปดํฌ๋„ŒํŠธ๋ฅผ

num1์ด๋ผ๋Š” ๋ณ€์ˆ˜์— ์ €์žฅํ•ด๋‘๊ณ  ๋ฐ˜๋ณต๋˜๋Š” ์ฝ”๋“œ๋ฅผ ์ค„์˜€์Œ

=> ์ด๋ ‡๊ฒŒ ๋ณ€์ˆ˜๋กœ ์ €์žฅํ•ด๋‘๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์„ ๊ถŒ์žฅ

 

โšก ์‹ฌํ™”๋‚ด์šฉ

์ฝ”ํ‹€๋ฆฐ์—์„œ val์€ ๋ณ€ํ•˜์ง€ ์•Š๋Š” ๋ณ€์ˆ˜! (์ƒ์ˆ˜์™€๋Š” ๋˜ ๋‹ค๋ฅธ ๊ฐœ๋…)

๋”ฐ๋ผ์„œ, ์•„๋ฌด๋ ‡๊ฒŒ๋‚˜ ๋ณ€ํ•  ์ˆ˜ ์žˆ๋Š” ๋ณ€์ˆ˜๋Š” var๋กœ ์„ ์–ธ!!


โœ… ์˜ค๋Š˜์˜ ๋ฌธ์ œ : 6๊ฐœ ๋ฒ„ํŠผ ํ…์ŠคํŠธ, ์ƒ‰๊น” ๋ณ€๊ฒฝํ•˜๊ธฐ

๐Ÿ‘€ activity_main.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">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="32dp"
        android:layout_marginBottom="41dp"
        android:clickable="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@android:drawable/ic_menu_rotate" />

    <LinearLayout
        android:layout_width="408dp"
        android:layout_height="730dp"
        android:gravity="center"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <Button
            android:id="@+id/lottoNum1"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#E0E0E0"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/lottoNum2"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#E0E0E0"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/lottoNum3"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#E0E0E0"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/lottoNum4"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#E0E0E0"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/lottoNum5"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#E0E0E0"
            app:cornerRadius="80dp" />

        <Button
            android:id="@+id/lottoNum6"
            android:layout_width="70dp"
            android:layout_height="80dp"
            android:backgroundTint="#E0E0E0"
            app:cornerRadius="80dp" />
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

 

๐Ÿ‘€ MainActivity.kt

package com.example.lotto

import android.content.res.ColorStateList
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.google.android.material.floatingactionbutton.FloatingActionButton

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val num1 = findViewById<Button>(R.id.lottoNum1)
        val num2 = findViewById<Button>(R.id.lottoNum2)
        val num3 = findViewById<Button>(R.id.lottoNum3)
        val num4 = findViewById<Button>(R.id.lottoNum4)
        val num5 = findViewById<Button>(R.id.lottoNum5)
        val num6 = findViewById<Button>(R.id.lottoNum6)

        findViewById<FloatingActionButton>(R.id.floatingActionButton).setOnClickListener{
            num1.text="43"
            num1.backgroundTintList=ColorStateList.valueOf(Color.rgb(255,0,0))

            num2.text="44"
            num2.backgroundTintList=ColorStateList.valueOf(Color.rgb(255,128,0))

            num3.text="25"
            num3.backgroundTintList=ColorStateList.valueOf(Color.rgb(255,255,0))

            num4.text="11"
            num4.backgroundTintList=ColorStateList.valueOf(Color.rgb(0,255,0))

            num5.text="8"
            num5.backgroundTintList=ColorStateList.valueOf(Color.rgb(0,0,255))

            num6.text="17"
            num6.backgroundTintList=ColorStateList.valueOf(Color.rgb(127,0,255))

        }
    }
}

๐Ÿ‘€ ์‹คํ–‰๊ฒฐ๊ณผ

https://codemate.kr/@guswlsdl04/๋ชจ๊ฐ์ฝ”-์ฝ”ํ‹€๋ฆฐ-๊ธฐ์ดˆํŽธ-7์ผ์ฐจ CLICK HERE!