Android sample code, kotlin

Basic Dialogs

Basic Dialogs

寫一個 App, 各式各樣的模組化 Dialog 是很重要的。

Text input dialog
Text input dialog

這裡先介紹最基本的二個 Dialog:

1. 使用 AlertDialog 來建立 Yes / No Dialog

臨時需要一個最簡單的 Dialog, 不需要拉 xml 檔,可以這麼作:

    private var okCancelAlertDialog: AlertDialog? = null

    interface ConfirmDialogCallback {
        fun onResult(isOK: Boolean)
    }

    fun launchOkCancelAlertDialog(
        context: Context,
        title: String?,
        message: String?,
        okStr: String,
        cancelStr: String?,
        callback: ConfirmDialogCallback
    ) {
        okCancelAlertDialog?.dismiss() // dismiss previous dialog if exists

        val alert = AlertDialog.Builder(context)
        alert.setTitle(title)
        alert.setMessage(message)

        alert.setPositiveButton(
            okStr
        ) { _, _ ->
            callback.onResult(true)
            okCancelAlertDialog?.dismiss()
            okCancelAlertDialog = null
        }

        if(cancelStr != null) {
            alert.setNegativeButton(
                cancelStr
            ) { _, _ ->
                callback.onResult(false)
                okCancelAlertDialog?.dismiss()
                okCancelAlertDialog = null
            }
        }

        okCancelAlertDialog = alert.create()
        alert.show()
    }

這個 Dialog 使用預設的版型,不用另外設計 xml 檔案。 Debug 或是工具用 APP 很實用。使用的範例:

        IotecKotlinDialogUtil.launchOkCancelAlertDialog(this,
            "Hello World",
            "Can you hear me?",
            "Yes",
            "No",
            object:IotecKotlinDialogUtil.Companion.ConfirmDialogCallback
            {
            @SuppressLint("SetTextI18n")
            override fun onResult(isOK: Boolean) {
                val tvAnswer1 = findViewById<TextView>(R.id.tv_answer1)
                if(isOK){
                    tvAnswer1.text = "Yes I heard you."
                }else{
                    tvAnswer1.text = "Nope. What did you said?"
                }
            }
        })

2. 可輸入文字的 Dialog

如果要讓 User 輸入文字,又懶得拉 xml,可以這麼作:

        private var textInputDialog: AlertDialog? = null


        interface TextDialogCallback {
            fun onTextInput(isOK: Boolean, inputText: String?)
        }

        fun launchTextInputDialog(
            context: Context,
            title: String?,
            defaultMessage: String?,
            hint:String?,
            okStr: String,
            cancelStr: String?,
            callback: TextDialogCallback
        ) {
            textInputDialog?.dismiss()

            val alert = AlertDialog.Builder(context)
            alert.setTitle(title)

            val edittext = EditText(context)
            edittext.setText(defaultMessage)
            edittext.hint = hint
            alert.setView(edittext)

            alert.setPositiveButton(okStr
            ) { _, _ ->
                val inputText = edittext.text.toString()
                callback.onTextInput(true, inputText)
                textInputDialog?.dismiss()
                textInputDialog = null
            }
            if (cancelStr != null) {
                alert.setNegativeButton(cancelStr
                ) { _, _ ->
                    callback.onTextInput(false, null)
                    textInputDialog?.dismiss()
                    textInputDialog = null
                }
            }
            textInputDialog = alert.create()
            textInputDialog?.show()
        }

呼叫的方式也很簡單:

        IotecKotlinDialogUtil.launchTextInputDialog(
            this,
            "Input user name",
            "",
            "What's your name?",
            "OK",
            "Cancel",
            object:IotecKotlinDialogUtil.Companion.TextDialogCallback{
                @SuppressLint("SetTextI18n")
                override fun onTextInput(isOK: Boolean, inputText: String?) {
                    val tvAnswer2 = findViewById<TextView>(R.id.tv_answer2)
                    if(isOK){
                        tvAnswer2.text = "Hello $inputText."
                    }else{
                        tvAnswer2.text = "I cannot hear you."
                    }
                }
            }
        )

3. 三個選項的基本 Dilaog

在不另外刻 xml 的前題下,大概能作到的就是三個選項

        private var threeOptionDialog: AlertDialog? = null
        enum class ThreeOptionsDialogSelection {
            OPTION1, OPTION2, OPTION3
        }

        interface ThreeOptionDialogCallback {
            fun onResult(option: ThreeOptionsDialogSelection)
        }

        fun launchThreeOptionDialog(
            context: Context,
            title: String?,
            message: String?,
            option1: String,
            option2: String?,
            option3: String?, callback: ThreeOptionDialogCallback
        ) {
            threeOptionDialog?.dismiss()

            val alert = AlertDialog.Builder(context)
            alert.setTitle(title)
            if (message != null) alert.setMessage(message)
            alert.setPositiveButton(
                option1
            ) { _, _ ->
                callback.onResult(ThreeOptionsDialogSelection.OPTION1)
                threeOptionDialog?.dismiss()
                threeOptionDialog = null
            }

            if(option2 != null) {
                alert.setNegativeButton(
                    option2
                ) { _, _ ->
                    callback.onResult(ThreeOptionsDialogSelection.OPTION2)
                    threeOptionDialog?.dismiss()
                    threeOptionDialog = null
                }
            }

            if(option3 != null) {
                alert.setNeutralButton(
                    option3
                ) { _, _ ->
                    callback.onResult(ThreeOptionsDialogSelection.OPTION3)
                    threeOptionDialog?.dismiss()
                    threeOptionDialog = null
                }
            }

            threeOptionDialog = alert.create()
            threeOptionDialog?.show()
        }

使用範例

        IotecKotlinDialogUtil.launchThreeOptionDialog(this,
        "Party invitation",
        "Will you go to the party?",
            "Yes!",
            "Nope",
            "Maybe",
            object:IotecKotlinDialogUtil.Companion.ThreeOptionDialogCallback{
                @SuppressLint("SetTextI18n")
                override fun onResult(option: IotecKotlinDialogUtil.Companion.ThreeOptionsDialogSelection) {
                    val tvAnswer3 = findViewById<TextView>(R.id.tv_answer3)
                    when(option){
                        OPTION1 -> tvAnswer3.text = "That's great!"
                        OPTION2 -> tvAnswer3.text = "Oh! That's pity!"
                        OPTION3 -> tvAnswer3.text = "Come on~ Let's go~"
                    }
                }
            }
        )

Sample project :

https://github.com/orangedream/IotecDialogExample

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.