Android Studio Kotlin 中的 AlertDialog 未显示

AlertDialog in Android Studio Kotlin not displaying

我知道提出这个问题听起来很傻,但我无法在使用 android studio 和 kotlin 开发的应用程序中显示 AlertDialog 框。这是代码:

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    runBlocking {
        showDefaultDialog(this)

    }
    FuelManager.instance.basePath = getString(R.string.BaseURL)

和功能:

private fun showDefaultDialog(context: Context) {
    val alertDialog = AlertDialog.Builder(context)

    alertDialog.apply {
        //setIcon(R.drawable.ic_hello)
        setTitle("Hello")
        setMessage("I just wanted to greet you. I hope you are doing great!")
        setPositiveButton("Positive") { _: DialogInterface?, _: Int ->
            Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show()
        }
        setNegativeButton("Negative") { _, _ ->
            Toast.makeText(context, "Negative", Toast.LENGTH_SHORT).show()
        }
        setNeutralButton("Neutral") { _, _ ->
            Toast.makeText(context, "Neutral", Toast.LENGTH_SHORT).show()
        }
    }.create().show()
}

我missing/doing哪里错了?我试过使用没有功能的代码,但也没有用。

(当我运行应用程序处于调试模式并在函数中设置断点时,断点被命中,但没有出现警报对话框!)

请帮忙。

编辑:

我试过这个(在测试应用中):

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    showDefaultDialog(this)
    Toast.makeText(this, "Hello!!!", Toast.LENGTH_SHORT).show()
}

显示对话框,但 Toast.makeText 也显示,无需等待 alertDialog 上的操作。

如何让alertDialog阻塞? (这是我之前代码的问题吗?)

您绝不能在主线程上阻塞,因此即使这样做有效,它也会使您的应用程序冻结并崩溃,并出现可怕的“应用程序无响应”对话框。在 onCreate() 中删除您的 Toast 调用。如果你想在对话框关闭后做一些事情,你必须把它放在你添加到对话框构建器的监听器中,比如 setOnDismissListener.

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        showDefaultDialog(this)
    }
}

private fun showDefaultDialog(context: Context) {
    val alertDialog = AlertDialog.Builder(context)

    alertDialog.apply {
        //setIcon(R.drawable.ic_hello)
        setTitle("Hello")
        setMessage("I just wanted to greet you. I hope you are doing great!")
        setPositiveButton("Positive") { _: DialogInterface?, _: Int ->
            Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show()
        }
        setNegativeButton("Negative") { _, _ ->
            Toast.makeText(context, "Negative", Toast.LENGTH_SHORT).show()
        }
        setNeutralButton("Neutral") { _, _ ->
            Toast.makeText(context, "Neutral", Toast.LENGTH_SHORT).show()
        }
        setOnDismissListener {
            Toast.makeText(context, "Hello!!!", Toast.LENGTH_SHORT).show()
        }

    }.create().show()
}

如果您需要处理 activity 私有的内容,那么您可以让此函数进行回调:

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        showDefaultDialog(this) {
            // put stuff that happens after dialog closed in this lambda
            Toast.makeText(this@MainActivity, "Hello!!!", Toast.LENGTH_SHORT).show()
        }
    }
}

inline fun showDefaultDialog(context: Context, crossinline onDismiss: ()->Unit) {
    val alertDialog = AlertDialog.Builder(context)

    alertDialog.apply {
        //setIcon(R.drawable.ic_hello)
        setTitle("Hello")
        setMessage("I just wanted to greet you. I hope you are doing great!")
        setPositiveButton("Positive") { _: DialogInterface?, _: Int ->
            Toast.makeText(context, "Hello", Toast.LENGTH_SHORT).show()
        }
        setNegativeButton("Negative") { _, _ ->
            Toast.makeText(context, "Negative", Toast.LENGTH_SHORT).show()
        }
        setNeutralButton("Neutral") { _, _ ->
            Toast.makeText(context, "Neutral", Toast.LENGTH_SHORT).show()
        }
        setOnDismissListener {
            onDismiss()
        }

    }.create().show()
}