扩展AlertDialog时如何解决“添加内容之前必须先调用requestFeature()”的问题

时间:2019-01-30 02:55:12

标签: android exception kotlin alertdialog

我正在尝试创建我的自定义AlertDialog,并得到“添加内容之前必须先调用requestFeature()”的异常

有人可以帮助我吗?

CustomAlertDialog:

class CustomConfirmDialog(
    internal var context: Context,
    // val title: String,
    val msg: String,
    val listener: OnConfirmListener
) : AlertDialog(context) {
    interface OnConfirmListener {
        fun onConfirmClick()
        fun onCancelClick()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.confirm_dialog)

        // tvDialogTitle.text = title
        tvDialogMsg.text = msg

        btnCancel.setOnClickListener {
            listener.onCancelClick()
            if (isShowing)
                dismiss()
        }

        btnOK.setOnClickListener {
            listener.onConfirmClick()
            if (isShowing)
                dismiss()
        }
    }

    fun setButtonText(strConfirm: String, strCancel: String) {
        btnOK?.text = strConfirm
        btnCancel?.text = strCancel
    }
}

这是呼叫代码

private fun stopAcc() {
    val msg = ResUtils.getStringFromRes(R.string.acc_stop_acc_prompt_msg)
    CustomConfirmDialog(activity!!, msg, listener).apply {
        setButtonText("stopAcc", "startGame")
        setCancelable(false)
        show()
    }
}

0 个答案:

没有答案