Android 自定义对话框不显示标题和消息

Android custom dialog not displaying title and message

我有一个自定义对话框,但标题和消息都没有显示:

import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.sibela.horn.R

class CarYearDialog : AppCompatDialogFragment() {

    private val args: CarYearDialogArgs by navArgs()
    private val car by lazy { args.car }

    private val rootView by lazy { View.inflate(activity, R.layout.dialog_car_year, null) }
    private val yearInput by lazy { rootView.findViewById<TextView>(R.id.year_input) }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = setupDialog()
        val dialog = builder.create()
        dialog.setCanceledOnTouchOutside(false)
        return dialog
    }

    private fun setupDialog(): AlertDialog.Builder {
        return AlertDialog.Builder(requireActivity(), R.style.AlertDialogTheme)
            .setTitle("Ano")
            .setMessage("Escolha o ano do veículo")
            .setView(rootView)
            .setNegativeButton("Cancelar") { _, _ -> }
            .setPositiveButton("Ok") { _, _ ->
                val year = yearInput.text.toString().toInt()
                val directions =
                    CarYearDialogDirections.actionCarYearDialogToHornToContactsFragment(car, year)
                findNavController().navigate(directions)
            }
    }
}

您可以使用 MaterialAlertDialogBu​​ilder。

import android.app.Dialog
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.navigation.fragment.findNavController
import androidx.navigation.fragment.navArgs
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.sibela.horn.R

class CarYearDialog : AppCompatDialogFragment() {

    private val args: CarYearDialogArgs by navArgs()
    private val car by lazy { args.car }

    private val rootView by lazy { View.inflate(activity, R.layout.dialog_car_year, null) }
    private val yearInput by lazy { rootView.findViewById<TextView>(R.id.yearinput) }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val builder = setupDialog()
        val dialog = builder.create()
        dialog.setCanceledOnTouchOutside(false)
        return dialog
    }

    private fun setupDialog(): AlertDialog.Builder {
        return MaterialAlertDialogBuilder(this.requireContext())
            .setTitle("Ano")
            .setMessage("Escolha o ano do veículo")
            .setView(rootView)
            .setNegativeButton("Cancelar") { ,  -> }
            .setPositiveButton("Ok") { , _ ->
                val year = yearInput.text.toString().toInt()
                val directions =
                    CarYearDialogDirections.actionCarYearDialogToHornToContactsFragment(car, year)
                findNavController().navigate(directions)
            }
    }
}