android - AutoCompleteTextView - 当我们在过滤后有一个完全匹配项时如何禁用下拉列表?

标签 android android-layout

有没有简单的方法来做到这一点? 因为只有一个元素与键入的文本相同的下拉菜单看起来多余。

我的适配器很简单,这是代码

AutoCompleteTextView autoCompleteTextViewAddress;
...
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(AvatarMainActivity.this, android.R.layout.simple_list_item_1, emailsSet.toEmailStringSet());
    autoCompleteTextViewAddress.setAdapter(adapter);

emailsS​​et.toEmailStringSet() 返回字符串集。

当我用与字符串集中相同的电子邮件填充 autoCompleteTextViewAddress 时,我仍然可以查看包含一个元素的下拉列表。

最佳答案

丑陋的解决方案,但它有效:

public class CustomAutoCompleteTextView extends AutoCompleteTextView {

    public CustomAutoCompleteTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
    public CustomAutoCompleteTextView(Context context, AttributeSet attrs)
    {
        super(context,attrs);
    }
    public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle)
    {
        super(context,attrs,defStyle);
    }
    @Override
    public boolean enoughToFilter()
    {
        boolean isEnough=(getThreshold()<=this.getText().length());

        if(isEnough)
        {
            if(this.getAdapter()!=null)
            {
                int itemsCount=0;
                int matchIndex=0;
                String txt = this.getText().toString();
                for (int i=0; i< this.getAdapter().getCount();i++) 
                {
                    String dat = (String)this.getAdapter().getItem(i);
                    if(dat.startsWith(txt))
                    {
                        itemsCount++;
                        matchIndex=i;
                    }
                }
                if(itemsCount == 1)
                {
                     if(((String)getAdapter().getItem(matchIndex)).equals(txt))
                     {
                         isEnough=false;
                     }

                }
            }
        }
        return isEnough;

    }


}

使用自定义类而不是原始的 AutoCompleteTextView

重写的 enoughToFilter 函数在我们的适配器中只有一个匹配记录时隐藏下拉列表

关于android - AutoCompleteTextView - 当我们在过滤后有一个完全匹配项时如何禁用下拉列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11969364/

相关文章:

android - 前一个 fragment 的 onSaveInstanceState 调用当前 fragment 的 on-orientation

android - Gradle Android Studio 集成

Android googlemap 内存不足

java - 将 json 解析为 java 对象时出错 : java. lang.NullPointerException

android - 在android中动态地将字体添加到textview中

android - sprite碰撞检测中心区域sprite

基于 Java 的 Android 应用程序 -> 切换到 Scala

java - 如何解决 "The content of the adapter has changed but ListView did not receive a notification”异常

android - 带有四个可点击区域的设计圆圈

android - ListView 在较小的手机中出现裁剪