반응형

 

다이얼로그를 일단 생성하고 오자

 

 

[KOTLIN] 다이얼로그 Dialog 생성

1. layout에 custom_dialog 생성 2. 아래와 같이 대략적인 다이얼로그가 실행되면 보일 화면 툴을 생성한다. //기존에 메인액티비티 레이아웃에 만들어놨던 버튼 동작 코드 val writeButton = findViewById(R.id.wr

chainterior.tistory.com

 

 

val mAlertDialog = mBuilder.show()

            mAlertDialog.findViewById<Button>(R.id.dateSelectBtn)?.setOnClickListener {

                val today = GregorianCalendar()
                val year : Int = today.get(Calendar.YEAR)
                val month : Int = today.get(Calendar.MONTH)
                val date : Int = today.get(Calendar.DATE)

                val dlg = DatePickerDialog(this, object : DatePickerDialog.OnDateSetListener {

                    //object alt+enter 하면 나오는 코드들
                    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int
                    ) {
                        //실행시킨 뒤에 날짜선택 후 로그가 잘찍히는지 확인해보자
                        Log.d("MAIN", "${year}, ${month + 1}, ${dayOfMonth}")
                    }

                }, year, month, date)
                dlg.show()

            }

 

 

해당 날짜를 누르면, 버튼에 써있는 글자를 해당 날짜로 변경해주는 코드를 추가 + 코드 정리

 

val mAlertDialog = mBuilder.show()

            //DateSelectBtn 코드를 깔끔하게 정리했다.
            val DateSelectBtn = mAlertDialog.findViewById<Button>(R.id.dateSelectBtn)

            DateSelectBtn?.setOnClickListener {

                val today = GregorianCalendar()
                val year : Int = today.get(Calendar.YEAR)
                val month : Int = today.get(Calendar.MONTH)
                val date : Int = today.get(Calendar.DATE)

                val dlg = DatePickerDialog(this, object : DatePickerDialog.OnDateSetListener {

                    //object alt+enter 하면 나오는 코드들
                    override fun onDateSet(view: DatePicker?, year: Int, month: Int, dayOfMonth: Int
                    ) {
                        //실행시킨 뒤에 날짜선택 후 로그가 잘찍히는지 확인해보자
                        Log.d("MAIN", "${year}, ${month + 1}, ${dayOfMonth}")
                        //버튼 글자를 해당 누른 날짜로 변경해주는 코드
                        DateSelectBtn.setText("${year}. ${month + 1}. ${dayOfMonth}")
                    }

                }, year, month, date)
                dlg.show()

            }

 

잘 나온다.

반응형