首页 > 解决方案 > AlertDialog 隐藏 AutoCompleteTextView 下拉菜单

问题描述

在我的表单中,有两个由自定义适配器填充的 AutoCompleteTextView。但是搜索需要一两秒钟,所以我想在 AlertDialog 中向用户显示加载动画。为了实现这一点,我在搜索适配器中声明了一个布尔 LiveData 变量,并在活动中观察它。我的目标是,当观察到的变量为真时显示 AlertDialog 并在变量为假时关闭。所以这是非常困难的。

这是显示对话框的代码片段

// To simplfy question I am using standart AlertDialog, the same problem occurs here,too
        val builder: AlertDialog.Builder? = AlertDialog.Builder(this, R.style.AlertDialogCustom)
        }
        builder?.setMessage("Results are loading")
                ?.setTitle("Loading")
        val dialog: AlertDialog? = builder?.create()

        SearchAdapter.isFetching.observe(this, Observer<Boolean> { isFetching ->
            isFetching?.let {
                if (it) {
                    dialog?.show()
                } else {
                    dialog?.hide()
                }
            }})

问题是当dialog?.show()名为 AutoCompleteTextView 的建议列表的代码不可见时。如果我评论该行,除了 AlertDialog (因为我评论了show()命令)之外,一切都可以正常工作。

我捕获了两个视频,首先dialog?.show()评论了一行并且 AutoCompleteTextView 工作正常

在第二dialog?.show()行没有评论,AutoCompleteTextView 没有按预期工作。

我无法弄清楚 AutoCompleteTextView 的下拉菜单和 AlertDialog 之间有什么问题,希望您能帮助我找出答案。

额外:AlertDialogCustom 样式仅用于将 TextColor 更改为白色以在由主题强制的黑色背景上显示消息。

<style name="AlertDialogCustom" parent="Theme.MaterialComponents.Dialog.Alert">
        <item name="android:colorAccent">@color/white</item>
    </style>

标签: androidkotlinandroid-alertdialogautocompletetextview

解决方案


推荐阅读