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

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

问题描述

我的布局中有一个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,请子类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天全站免登陆