[TIL] #32 Bottom Sheet Dialog

Yeon·2023년 9월 5일
0

내일배움캠프 - Kotlin

목록 보기
43/58
post-thumbnail

1. Xml Code

<?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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/ice_blue"
    android:padding="16dp">

    <EditText
        android:id="@+id/dialogbottom_edittext"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Enter Text"
        android:inputType="text"
        android:maxLines="1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/dialogbottom_btn_cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:backgroundTint="@color/pale_gray"
        android:text="Cancel"
        android:textColor="@color/charcoal_black"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/dialogbottom_edittext" />

    <Button
        android:id="@+id/dialogbottom_btn_save"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:backgroundTint="@color/pale_gray"
        android:text="Save"
        android:textColor="@color/charcoal_black"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/dialogbottom_edittext" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. Kotlin Code

class BottomSheetDialog : BottomSheetDialogFragment() {
    private lateinit var editText: EditText
    private var selectedTextView: TextView? = null

    companion object {
        const val TAG = "BottomSheetDialog"

        fun newInstance(text: String, selectedTextView: TextView): BottomSheetDialog {
            val fragment = BottomSheetDialog()
            val args = Bundle()
            args.putString(ARG_TEXT, text)
            fragment.arguments = args
            fragment.selectedTextView = selectedTextView
            return fragment
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        val binding = DialogBottomSheetBinding.inflate(inflater, container, false)
        val view = binding.root

        editText = binding.dialogbottomEdittext
        val saveButton = binding.dialogbottomBtnSave
        val cancelButton = binding.dialogbottomBtnCancel

        // 전달된 텍스트 설정
        editText.setText(arguments?.getString(ARG_TEXT))
        editText.setSelection(editText.text.length)

        saveButton.setOnClickListener {
            // 수정된 텍스트를 호출한 TextView에 설정
            val updatedText = editText.text.toString()
            selectedTextView?.text = updatedText
            dismiss()
        }

        cancelButton.setOnClickListener {
            dismiss()
        }

        return view
    }
}

0개의 댓글