android-studio - Android Studio Kotlin 中的 AlertDialog 不显示

标签 android-studio kotlin android-alertdialog

我知道我提出这个问题听起来很愚蠢,但我根本无法在使用 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()
}

我错过了什么/做错了什么?我尝试过使用不带该功能的代码,但也不起作用。

(当我在 Debug模式下运行应用程序并在函数中设置断点时,断点被命中,但警报对话框没有显示!)

请帮忙。

编辑:

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

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 私有(private)的操作进行处理,那么您可以让此函数进行回调:

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()
}

关于android-studio - Android Studio Kotlin 中的 AlertDialog 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70757388/

相关文章:

Android Studio 3.5重构问题

android - Bytedeco的Tesseract库是通过gradle依赖添加的,但是没有找到

android - 迁移到Gradle插件3.2.1

android - 标记对拖动不是很敏感

android - 尝试从静态 AsyncTask 方法显示 ProgressDialog 时出现 NullPointerException

android - 需要 Gradle 版本 2.2。当前版本是 4.4 错误

android - Android Studio 在 build.gradle 中默认构建的 Product Flavor 是什么?

android - 在我的 kotlin/android 代码中,联系人姓名未显示在收件箱中

android - 如何在自定义对话框中设置自定义按钮?

android - 如何在对话框中添加多个 TextView