AutoCompleteTextView.isPopupShowing()始终为FALSE

问题描述 投票:4回答:2

我试图找出是否显示/隐藏了AutoCompleteTextView下拉列表。在按钮上单击我想显示下拉列表(如果它是隐藏的),并隐藏它(如果它显示)。为此我使用方法isPopupShowing(),但它总是返回FALSE。

例:

@Override
public void onClick(View view) {

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

        if (autoCompleteTextView.isPopupShowing()) {
            autoCompleteTextView.dismissDropDown();
        } else {
            autoCompleteTextView.showDropDown();
        }   
    }   
}
android autocompletetextview
2个回答
3
投票

当AutoCompleteTextView失去焦点时,下拉列表消失。因此,单击按钮时,下拉列表始终不可见。

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


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.