Moments of growth

[Kotlin] Dialog setOnClickListener ๋ณธ๋ฌธ

Android [Kotlin] ๐Ÿ’ป๐Ÿค

[Kotlin] Dialog setOnClickListener

๋ฎค๋ง์ด 2021. 11. 20. 00:01

Dialog Fragment ๋„์šฐ๊ณ  ์ทจ์†Œ, ๋“ฃ๊ธฐ ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž

๐Ÿ“Œ ์ž˜๋ชป ๊ตฌํ˜„ํ•œ ์ฝ”๋“œ

binding.btnCreate.setOnClickListener{
    val DialogView = LayoutInflater.from(context).inflate(R.layout.fragment_create_dialog,null)
    val Builder = AlertDialog.Builder(context)
        .setView(DialogView)
    Builder.show()
}

 

โœ” ์ด๋ ‡๊ฒŒ ๊ตฌํ˜„ํ•  ๊ฒฝ์šฐ ์ƒˆ๋กœ์šด ๋ทฐ๊ฐ€ ์ƒ์„ฑ๋ผ์„œ CreateDialogFragment.kt์—์„œ ์ •์˜ํ•œ setOnClickListener๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค

-> ์ด ๋ถ€๋ถ„ ์ž˜ ์ดํ•ด๋ชปํ•จ (๋” ๊ณต๋ถ€ ํ•„์š”)

 

 

๐Ÿ“Œ InHomeFragment.kt (์˜ฌ๋ฐ”๋ฅธ ์ฝ”๋“œ)

binding.btnCreate.setOnClickListener{
    CreateDialogFragment().show(childFragmentManager,"")
}

โœ” show()๋กœ ๊ตฌํ˜„ํ•˜๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค

โœ” CreateDialogFragment.kt ์—์„œ ๊ตฌํ˜„ํ•œ ํด๋ฆญ๋ฆฌ์Šค๋„ˆ๋“ค์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค

 

 

๐Ÿ“Œ CreateDialogFragment.kt

class CreateDialogFragment : DialogFragment() {
    private var _binding: FragmentCreateDialogBinding? = null
    private val binding get() = _binding ?: error("์—๋Ÿฌ์—๋Ÿฌ")

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentCreateDialogBinding.inflate(
            layoutInflater,
            container,
            false
        )

        binding.btnCancel.setOnClickListener {
            dismiss()
        }
        
        binding.btnListen.setOnClickListener {
            val intent = Intent(requireContext(), ChattingActivity::class.java)
            startActivity(intent)
        }

        return binding.root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

}

โœ” dismiss() : ๋‹ค์ด์–ผ๋กœ๊ทธ๊ฐ€ ์‚ฌ๋ผ์ง

Comments