반응형
다이얼로그를 일단 생성하고 오자
[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()
}
잘 나온다.
반응형
'kotlin↗ > 안드로이드 스튜디오 메이킹' 카테고리의 다른 글
[코틀린] 버튼 클릭시 음악 재생 (0) | 2024.02.17 |
---|---|
[KOTLIN] 컬러 변경 및 컬러 색상 추가하기 (0) | 2024.02.07 |
[KOTLIN] 다이얼로그 Dialog 생성 (0) | 2024.02.07 |
[KOTLIN] Firebase 어플 실행 시 Splash 화면에서 비회원 로그인 받아주기 (0) | 2024.02.07 |
[KOTLIN] FIREBASE 파이어베이스 익명 로그인 (1) | 2024.02.06 |