以编程方式设置文本时,如何避免出现自动完成下拉列表? [英] How can I avoid autocomplete dropdown appearing when text is programmatically set?

查看:27
本文介绍了以编程方式设置文本时,如何避免出现自动完成下拉列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的布局中有一个 AutoCompleteTextView.我还有另一种方法来选择 AutoCompleteTextView 中存在的相同项目.选择替代方式后,我通过以下方式填充 AutoCompleteTextView 中的值:

I have an AutoCompleteTextView in my layout. I also have an alternative way to select the same items which are present in the AutoCompleteTextView. When the alternative way is selected, I populate the value in the AutoCompleteTextView via:

autoCompleteTextView.setText(valueFromAlternativeSource);

其中 valueFromAlternativeSource 是有效的自动完成选项之一.这样做的问题是在调用 setText 时会出现自动完成下拉列表.将以下行放在上面不起作用之后:

where valueFromAlternativeSource is one of the valid auto complete options. The trouble with this is that the autocomplete dropdown appears when setText is called. Putting the following line after the above doesn't work:

autoCompleteTextView.dismissDropDown();  //Doesn't work.  Why?

关于为什么关闭下拉菜单不起作用的任何想法或我可以关闭下拉菜单的其他方法?

Any ideas on why dismiss dropdown isn't working or other ways I could dismiss the dropdown?

推荐答案

如果你想支持 API<17,Subclass AutoCompleteTextview 并覆盖 setText(text, filter) 方法

If you want to support API<17, Subclass AutoCompleteTextview and override setText(text, filter) method

@Override
public void setText(CharSequence text, boolean filter) {
    if(Build.VERSION.SDK_INT>=17) {
        super.setText(text, filter);
    }else{
        if(filter){
            setText(text);
        }else{
            ListAdapter adapter = getAdapter();
            setAdapter(null);
            setText(text);
            if(adapter instanceof ArrayAdapter)
                setAdapter((ArrayAdapter) adapter);
            else
                setAdapter((CursorAdapter) adapter);
            //if you use more types of Adapter you can list them here
        }
    }
}

然后每当你想手动设置文本时调用 setText(text, false)

Then whenever you want to set the text manually call setText(text, false)

这篇关于以编程方式设置文本时,如何避免出现自动完成下拉列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆