#1 ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ์ฌ์ฉ ์
#1-1 ์์ ์ฑ

์๊ณผ ๊ฐ์ ๊ฐ๋จํ ์ฑ์ด ์๋ค. Button์ ๋๋ฅด๋ฉด, EditText์ text๊ฐ ๋ฐ๋ก ์์ ์๋ TextView์ text์ ๋์
๋๋ค. ์ด ์ฑ์ ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ๋ค.
#1-2 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">
<TextView
android:id="@+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="25dp"
android:textSize="30dp"
app:layout_constraintBottom_toTopOf="@id/my_container"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/my_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="@+id/my_edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Input any text to print"
android:inputType="text"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/my_button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/my_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Print"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/my_edit_text"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
#1-3 MainActivity.kt
// package com.example.databindingpractice
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myTextView = findViewById<TextView>(R.id.my_text_view)
val myEditText = findViewById<EditText>(R.id.my_edit_text)
val myButton = findViewById<Button>(R.id.my_button)
myButton.setOnClickListener{
myTextView.text = myEditText.text
}
}
}
์๋๋ก์ด๋๋ฅผ ์ฒ์ ๋ฐฐ์ธ ๋๋ถํฐ ์ฌ์ฉํ๋ findViewById()๊ฐ ์๋ค. findViewById()๋ view(๋ณด์ด๋ ํ๋ฉด)์ ์๋ ๋ ํผ๋ฐ์ค(์ฐธ์กฐ)๋ฅผ ํ๋ณดํ๋ ๋ฐฉ๋ฒ ์ค ๊ฐ์ฅ ๊ธฐ์ด์ ์ธ ๋ฐฉ๋ฒ์ด๋ค. findViewById()๋ ๋ฐํ์์ด ์๋๋ผ ์ปดํ์ผ ์์ ์ view์ ๋ ํผ๋ฐ์ค๋ฅผ ํ๋ณดํ๊ธฐ์, ์ ์ด๋ ๋ฐํ์ ์ค view์ ๊ดํ ์๋ฌ๋ ๋์ง ์๊ฒ ๋ณด์ฅํ๋ค๋ ์ฅ์ ์ด ์๋ค. ํ์ง๋ง, TextView์ ๊ฐ์ ์์ ฏ๋ค์ ๊ฐฏ์๊ฐ ๋ง์์ง๋ฉด, ํ๋ํ๋ findViewById()๋ฅผ ์ฐ๋ฉฐ ๋ทฐ ๊ณ์ธต์ ์ ๊ทผํ๋ ๊ฒ์ด ๋ฒ๊ฑฐ๋ก์์ง๋ค. ๋์์ ์ฝ๋์ ๊ฐ๋
์ฑ๋ ์ ํ๋๋ค.
์ด ๋, findViewById()์ ์ฅ์ ์ ์ ์งํ๋ฉด์, ๋จ์ ์ ํด์ํ๋ ๋ฐฉ๋ฒ์ด ์๋๋ฐ ๋ฐ๋ก Binding object๋ผ๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ด๋ค. Binding object๋ ๊ฐ layout์ ๋ค์ด์๋ ๋ชจ๋ view์ ๋ ํผ๋ฐ์ค๋ฅผ ์ ์ฅํ๋ ๊ฐ์ฒด๋ค.
#2 ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ๊ธฐ์ด์ ์ธ ์ฌ์ฉ
#2-1 build.gradle.kts (Module :app)์์ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐํ๊ธฐ
plugins {
...
}
android {
...
buildFeatures {
dataBinding = true
}
}
dependencies {
...
}
Binding object๋ฅผ ์ฌ์ฉํ๊ธฐ์ํด ๋จผ์ , Data Binding ๋ผ์ด๋ธ๋ฌ๋ฆฌ(androidx.databinding)๋ฅผ ๋ค์ด๋ก๋ํด์ผ ํ๋ค. ๋ชจ๋ ์์ค์ ๊ทธ๋๋ค ๋น๋ ํ์ผ build.gradle.kts (Module :app)์์ android {...} ์์, buildFeatures { dataBinding = true } ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ค. ๊ทธ๋๋ค ๋ฒ์ 4.0๋ฏธ๋ง์ ๋์ dataBinding { enabled = true } ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ค. ์ด๋ฌ๋ฉด gradle์ด ์์์ androidx.databinding ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ค์ด๋ก๋ ํ๋ค. ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ฉด ์๋จ์ Sync๋ฅผ ํ๋ผ๋ ์๋ด ๋ฉ์์ง๊ฐ ๋จ๋๋ฐ, ํด๋น ๋ฉ์์ง์ Sync ๋ฒํผ์ ๋๋ฅธ๋ค.
#2-2 activity_main.xml์ <layout> ํ๊ทธ ์์ฐ๊ธฐ
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
...
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
<?xml version="1.0" encoding="utf-8"?>๋ฅผ ์ ์ธํ ๋ชจ๋ ๋ถ๋ถ์ <layout> ํ๊ทธ๋ก ๊ฐ์ผ๋ค. <layout> ํ๊ทธ๋ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ ์ํ ๋ ์ด์์ ํ์ผ์์ ์๋๋ก์ด๋ ์์คํ
์ ์๋ ค์ค๋ค. ์ด๋ ๊ฒํ๋ฉด, Data Binding ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์๋์ผ๋ก Binding class๋ฅผ ์์ฑํ๋ค. ์ด์ ๋ถํฐ Binding class์์ Binding object๋ฅผ ์ฐ์ด๋ผ ์ ์๋ค. ์ด ํด๋์ค์ ์ด๋ฆ์ abc_def_ghi.xml๋ผ๋ฉด AbcDeFGhiBinding์ผ๋ก ์ ํด์ง๋ค. ์ฆ, _๋ก ๊ตฌ๋ถ๋๋ ๋จ์ด๋ค์ ์ฒซ๊ธ์๋ฅผ ๋๋ฌธ์๋ก ๋ฐ๊พผ ๋ค์ "Binding"์ด ๋ถ์ฌ์ง๋ค.
xmlns ์ฆ, xml ํ์ผ์ ๋ค์์คํ์ด์ค ์ ์ธ(namespace declarations)์ xml ํ์ผ์์ ๊ฐ์ฅ ์ธ๊ณฝ(outmost)์ ์๋ ํ๊ทธ์ ์์ฑ์ผ๋ก ๋ค์ด๊ฐ ์์ด์ผ ํ๋ค๋ ๊ท์น์ด ์์ผ๋ฏ๋ก, ์๋ ์๋ ์๋ฆฌ์์ <layout> ํ๊ทธ๋ก ์ฎ๊ฒจ์ค๋ค.
#2-3 MainActivity.kt์์ Binding object ์ฌ์ฉํ๊ธฐ
// package com.example.databindingpractice
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import com.example.databindingpractice.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding : ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = DataBindingUtil.setContentView(this, R.layout.activity_main)
binding.myButton.setOnClickListener{
binding.myTextView.text = binding.myEditText.text
}
}
}
Activity์์ ๋ฐ์ธ๋ฉ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ ๋ชจ์ต์ด๋ค. androidx.databinding.DataBindingUtil ํด๋์ค์ setContentView()๋ก ์ด๊ธฐํ๋ฅผ ํ๋ค. ์๋์ setContentView()๋ androidx.appcompat.app.AppCompatActivity์ ๋ฉ์๋์๋ค. ํ์ง๋ง, ์ด๋ฆ์ด ๊ฐ๋ค๋ ๊ฒ์์ ์ ์ ์๋ฏ ํ๋ ์ผ์ '๋น์ท'ํ ๊ฒ์์ ์ ์ถํ ์ ์๋ค. binding ๊ฐ์ฒด์๋ ActivityMainBinding ํด๋์ค์ ์์ ฏ id๋ค ์ฆ activity_main.xml์ ์์ ฏ id ์ ๋ณด๊ฐ ํ๋กํผํฐ๋ก์ ๋ด๊ฒจ์ ธ ์๋ค.
๊ทธ ํ๋กํผํฐ์ ์ด๋ฆ์ Binding ํด๋์ค์ ์ด๋ฆ ๊ท์น๊ณผ ๋น์ทํ๊ฒ ์๋ ์์ฑ๋์ด์๋ค. xml์์ ์์ ฏ์ android:id ์์ฑ์ด "@+id/abc_def_ghi"๋ก ์ค์ ๋์ด์๋ค๋ฉด abcDefGhi๊ฐ ํ๋กํผํฐ๋ช
์ด ๋๋ค.
๊ฒฐ๋ก ์ ์ผ๋ก, findViewById๋ก ๋ ํผ๋ฐ์ค๋ฅผ ํ๋ํ๋ ๋ถ๋ฌ์์ ๋ณ์ ์ด๋ฆ์ ํ๋ํ๋ ๋ถ์ด๋ ๋ฐฉ์์ ๋นํด ์์ค ์ฝ๋ ์์ฑ ์๋๊ฐ ๋ ๋นจ๋ผ์ก์ผ๋ฉฐ, ๊ฐ๋
์ฑ๋ ์ข์์ก๋ค.
#2-4 apply ํจ์ ์ฌ์ฉ
/*
binding.myButton.setOnClickListener{
binding.myTextView.text = binding.myEditText.text
}
*/
binding.apply {
myButton.setOnClickListener {
myTextView.text = myEditText.text
}
}
์ฝํ๋ฆฐ์ scope ํจ์๋ค ์ค ํ๋์ธ apply๋ฅผ ํ์ฉํ๋ฉด ์๊ณผ ๊ฐ์ด ์ฝ๋๋ฅผ ๋์ฑ ๊ฐ์ํ์ํฌ ์ ์๋ค.
#3 ์์ฝ
Data Binding์ผ๋ก findViewById()๋ฅผ ์ผ๊ด ์ค์ ํ ์ ์๋ค.
#4 ์์ฑ๋ ์ฑ
android-practice/data-binding/DataBindingBasics at master ยท Kanmanemone/android-practice
Contribute to Kanmanemone/android-practice development by creating an account on GitHub.
github.com
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] LiveData - ๊ธฐ์ด (0) | 2024.01.16 |
---|---|
[Android] ViewModel - ๋ทฐ ๋ชจ๋ธ์ ์ธ์(Argument) ์ ๋ฌ (ViewModelFactory) (0) | 2024.01.15 |
[Android] ViewModel - ๊ธฐ์ด (0) | 2024.01.13 |
[Android] Data Binding - View์ ๊ฐ์ฒด ์ ๋ฌ (0) | 2024.01.12 |
AndroidX (๊ตฌ Support Library) (0) | 2023.12.13 |