๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | Dialog

hyihyiยท2024๋…„ 1์›” 9์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
30/70
post-thumbnail

Dialog

1. ๊ธฐ๋ณธ ๋‹ค์ด์–ผ๋กœ๊ทธ(AlertDialog)

// 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. ์ปค์Šคํ…€ ๋‹ค์ด์–ผ๋กœ๊ทธ(CustomDialog)

// 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. ๋‚ ์งœ ๋‹ค์ด์–ผ๋กœ๊ทธ(DatePickerDialog)

// 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. ์‹œ๊ฐ„ ๋‹ค์ด์–ผ๋กœ๊ทธ(TimePickerDialog)

// 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. ์ง„ํ–‰ ๋‹ค์ด์–ผ๋กœ๊ทธ(ProgressDialog)

// 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>

profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€