From e6483b856527b62cc3b9ddf2dcf7554dedf7a208 Mon Sep 17 00:00:00 2001 From: jinsu4755 Date: Thu, 31 Dec 2020 13:45:19 +0900 Subject: [PATCH] =?UTF-8?q?[Fix]=20=EA=B0=95=EC=9D=98=20=EC=9E=90=EB=A3=8C?= =?UTF-8?q?=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../practicegitanddatabinding/MainActivity.kt | 32 +++++++++++++------ app/src/main/res/layout/activity_main.xml | 13 +++----- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/sotp/semina/practicegitanddatabinding/MainActivity.kt b/app/src/main/java/sotp/semina/practicegitanddatabinding/MainActivity.kt index 9d56f6c..85d8dde 100644 --- a/app/src/main/java/sotp/semina/practicegitanddatabinding/MainActivity.kt +++ b/app/src/main/java/sotp/semina/practicegitanddatabinding/MainActivity.kt @@ -4,30 +4,42 @@ import android.os.Bundle import android.widget.EditText import androidx.appcompat.app.AppCompatActivity import androidx.databinding.DataBindingUtil -import androidx.databinding.ObservableField import sotp.semina.practicegitanddatabinding.databinding.ActivityMainBinding class MainActivity : AppCompatActivity() { - - private lateinit var binding: ActivityMainBinding - val editText = ObservableField() + /* TODO step4 설계 변경이 일어납니다 binding 을 전역 클래스 맴버 변수로 올리고 + * 양방향 데이터 바인딩을 위한 ObservableField 를 작성합니다 + * 이 과정에서 하단 getEditTextToString이 사라질 수 있습니다.*/ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - binding = DataBindingUtil.setContentView(this, R.layout.activity_main) - binding.mainActivity = this + val binding: ActivityMainBinding = + DataBindingUtil.setContentView(this, R.layout.activity_main) binding.userProfile = UserData(IMAGE_URL, DEFAULT_USER_NAME) + /*TODO step2 엑티비티를 결합하였으므로 binding에 엑티비티를 결합한다.*/ + addButtonClickEvent(binding) } - private fun setProfileName(content: String) { - binding.userProfile = UserData(IMAGE_URL, content) + private fun setProfileName(binding: ActivityMainBinding, content: String) { + binding.userProfile = UserData(IMAGE_URL,content) } - fun changeUserNameEvent() { - setProfileName(editText.get() ?: DEFAULT_USER_NAME) + /*TODO step3 해당 함수를 버튼이 클릭 되었을때 어떤 일이 일어날지 수정합시다. + * 필요시 다른 함수를 수정해야함.*/ + private fun addButtonClickEvent(binding: ActivityMainBinding) { + binding.buttonProfileChangeButton.setOnClickListener { + setProfileName( + binding, + getEditTextToString(binding.inputChangedProfile) + ) + } } + + private fun getEditTextToString(content: EditText): String = content.text.toString() + + companion object { private const val IMAGE_URL = "https://avatars3.githubusercontent.com/u/45380072?s=460&u=b9fc82996ec2cc568a7dfcbf8846944dc16a7ccd&v=4" diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 1cf7192..6eebcf3 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -4,14 +4,11 @@ xmlns:tools="http://schemas.android.com/tools"> - - + + +