AutoCompleteTextView.isPopupShowing() 总是假的

AutoCompleteTextView.isPopupShowing() is always FALSE

我正在尝试查明 AutoCompleteTextView 下拉列表是否为 shown/hidden。单击按钮我想显示下拉菜单(如果它是隐藏的),并隐藏它(如果它显示)。为此,我使用方法 isPopupShowing(),但它总是返回 FALSE.

示例:

@Override
public void onClick(View view) {

    if (view.getId() == button.getId()) {

        if (autoCompleteTextView.isPopupShowing()) {
            autoCompleteTextView.dismissDropDown();
        } else {
            autoCompleteTextView.showDropDown();
        }   
    }   
}

当 AutoCompleteTextView 失去焦点时,下拉列表消失。所以当你点击按钮时,下拉菜单总是不可见的。

只需向您的侦听器添加一个新布尔值 属性 即可记住上次状态。

Kotlin 中的代码

val afill = findViewById<AutoCompleteTextView>(R.id.myTextId)
var showAFill = false

afill.addTextChangedListener (object : TextWatcher {
    override fun afterTextChanged(p0: Editable?) {
         showAFill = afill.isPopupShowing
    }
    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
    }
    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int)
    }
})

afill.onItemClickListener = object : AdapterView.OnItemClickListener {
    override fun onItemClick(parent: AdapterView<*>?, view: View, position: Int, id: Long) {
        showAFill = false
    }
}

如果您单击自动填充中的任何元素,它将关闭。

showAutofill.setOnClickListener { _ ->
    if (showAFill) afill.dismissDropDown()
    else afill.showDropDown()
    showAFill = !showAFill
}

showAutofill - 我的按钮在布局上的 ID