다이얼로그
* 다이얼로그
사용자에게 결정을 내리거나 추가정보를 입력하라는 메시지를 표시하는 작은창
보통은 사용자가 다음으로 계속 진행하기 전에 조치를 취해야 하는 모달 이벤트에 사용
* 다이얼로그 구조
알림 다이얼로그에는 총 세 가지 영역이 있음
1. 제목
- 선택 사항이며 콘텐츠 영역에 상세한 메시지,목록 또는 맞춤 레이아웃이 채워져 있는 경우에만 사용
- 단순 메시지 혹은 질문을 나타내는 경우 제목은 없어도 됨
2. 콘텐츠 영역
- 메시지,목록 또는 다른 맞춤 레이아웃을 표시 할 수 있음
3. 작업 버튼
- 대화 상자 하나에 작업 버튼은 최대 세 개
다이얼로그 예제
1. 기본 다이얼로그(AlertDialog)
binding.btn1Alert.setOnClickListener {
var builder = AlertDialog.Builder(this)
builder.setTitle("기본 다이얼로그 타이틀")
builder.setMessage("기본 다이얼로그 메세지")
builder.setIcon(R.mipmap.ic_launcher)
// 버튼 클릭시에 무슨 작업을 할 것인가!
val listener = object : DialogInterface.OnClickListener {
override fun onClick(p0: DialogInterface?, p1: Int) {
when (p1) {
// 해당 버튼을 클릭 시 textView의 text를 변경
DialogInterface.BUTTON_POSITIVE ->
binding.tvTitle.text = "BUTTON_POSITIVE"
DialogInterface.BUTTON_NEUTRAL ->
binding.tvTitle.text = "BUTTON_NEUTRAL"
DialogInterface.BUTTON_NEGATIVE ->
binding.tvTitle.text = "BUTTON_NEGATIVE"
}
}
}
builder.setPositiveButton("Positive", listener)
builder.setNegativeButton("Negative", listener)
builder.setNeutralButton("Neutral", listener)
builder.show()
}
2. 커스텀 다이얼로그(CustomDialog)
binding.btn2Custom.setOnClickListener {
val builder = AlertDialog.Builder(this)
builder.setTitle("커스텀 다이얼로그")
builder.setIcon(R.mipmap.ic_launcher)
val v1 = layoutInflater.inflate(R.layout.dialog,null)
builder.setView(v1)
val listener = DialogInterface.OnClickListener { dialog, which ->
val alert = dialog as AlertDialog
val edit1 : EditText? = alert.findViewById<EditText>(R.id.editText)
val edit2 : EditText? = alert.findViewById<EditText>(R.id.editText2)
binding.tvTitle.text = "이름 : ${edit1?.text}"
binding.tvTitle.append("/ 나이 : ${edit2?.text}")
}
builder.setPositiveButton("확인",listener)
builder.setNegativeButton("취소", null)
builder.show()
}
* 커스텀 다이얼로그(CustomDialog) xml
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_margin="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:text="Name"/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_margin="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText"
android:text="AGE"/>
3. 날짜 다이얼로그(DatePickerDialog)
binding.btn3Date.setOnClickListener {
val calendar = Calendar.getInstance()
val year = calendar.get(Calendar.YEAR)
val month = calendar.get(Calendar.MONTH)
val day = calendar.get(Calendar.DAY_OF_MONTH)
val listener = DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
binding.tvTitle.text = "${year}년 ${month}월 ${dayOfMonth}일"
}
var picker = DatePickerDialog(this, listener, year, month, day)
picker.show()
}
4. 시간 다이얼로그(TimePickerDialog)
binding.btn4Time.setOnClickListener {
val calendar = Calendar.getInstance()
val hour = calendar.get(Calendar.HOUR)
val minute = calendar.get(Calendar.MINUTE)
val listener = TimePickerDialog.OnTimeSetListener { view, hourOfDay, minute ->
binding.tvTitle.text = "${hourOfDay}시 ${minute}분"
}
var picker = TimePickerDialog(this, listener, hour, minute, false)
picker.show()
}
5. 진행 다이얼로그(ProgressDialog)
binding.btn5Progress.setOnClickListener {
val builder = AlertDialog.Builder(this)
builder.setTitle("프로그래스바")
builder.setIcon(R.mipmap.ic_launcher)
val v1 = layoutInflater.inflate(R.layout.progressbar, null)
builder.setView(v1)
builder.show()
}
*진행 다이얼로그(ProgressDialog) xml
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="match_parent"
android:layout_height="76dp"
android:layout_marginTop="48dp"
android:indeterminate="false"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..."
android:textSize="16dp"
android:layout_marginTop="16dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/progressbar"/>
'Android 앱개발 공부 > TIL(Today I Learned)' 카테고리의 다른 글
[Android] TIL 38일차 (1) | 2024.07.18 |
---|---|
[Android] TIL 37일차 (0) | 2024.07.16 |
[Android] TIL 35일차 (0) | 2024.07.12 |
[Android] TIL 34일차 (0) | 2024.07.11 |
[Android] TIL 33일차 (0) | 2024.07.10 |