// 1. ๊ธฐ๋ณธ ๋ค์ด์ผ๋ก๊ทธ
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) {
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. ์ปค์คํ
๋ค์ด์ผ๋ก๊ทธ
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)
// p0์ ํด๋น AlertDialog๊ฐ ๋ค์ด์จ๋ค. findViewById๋ฅผ ํตํด view๋ฅผ ๊ฐ์ ธ์์ ์ฌ์ฉ
val listener = DialogInterface.OnClickListener { p0, p1 ->
val alert = p0 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()
}
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
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:text="AGE" />
</LinearLayout>
</LinearLayout>
// 3. ๋ ์ง ๋ค์ด์ผ๋ก๊ทธ
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 { datePicker, i, i2, i3 ->
// i๋
i2์ i3์ผ
binding.tvTitle.text = "${i}๋
${i2 + 1}์ ${i3}์ผ"
}
var picker = DatePickerDialog(this, listener, year, month, day)
picker.show()
}
// 4. ์๊ฐ ๋ค์ด์ผ๋ก๊ทธ
binding.btn4Time.setOnClickListener {
val calendar = Calendar.getInstance()
val hour = calendar.get(Calendar.HOUR)
val minute = calendar.get(Calendar.MINUTE)
val listener = TimePickerDialog.OnTimeSetListener { timePicker, i, i2 ->
binding.tvTitle.text = "${i}์ ${i2}๋ถ"
}
val picker = TimePickerDialog(this, listener, hour, minute, false) // trueํ๋ฉด 24์๊ฐ ์
picker.show()
}
// 5. ํ๋ก๊ทธ๋ ์ค ๋ค์ด์ผ๋ก๊ทธ
// ๊ถ์ฅํ์ง ์์ง๋ง ์ฌ์ฉ์ ๊ฐ๋ฅํ๋ค.
binding.btn5Porgress.setOnClickListener {
pro = ProgressDialog.show(this, "ํ์ดํ์
๋๋ค.", "๋ฉ์์ง์
๋๋ค.")
// ํธ๋ค๋ฌ๋ฅผ ํตํด์ ์ข
๋ฃ ์์
์ ํ๋ค.
val handler = Handler()
val thread = Runnable { pro?.cancel() }
handler.postDelayed(thread, 5000) // ๋๋ ์ด๋ 5์ด
}
//6. ํ๋ก๊ทธ๋์ค ๋ค์ด์ผ๋ก๊ทธ ๋ค๋ฅธ ๋ฐฉ์. (์ปค์คํ
๊ณผ ๋น์ท)
binding.btn5Porgress.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()
}
progressbar.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="match_parent"
android:layout_height="76dp"
android:layout_gravity="center"
android:layout_marginTop="48dp"
android:indeterminate="false"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loading..."
android:textSize="16sp"
android:layout_marginTop="16dp"/>
</LinearLayout>