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

android – AutoCompleteTextView – 当我们在过滤后有一个完全匹配时如何禁用下拉列表?,第1张

概述有一个简单的方法吗? 因为与一个与键入文本相同的元素的下拉列表看起来是多余的. 我的适配器很简单,这是代码 AutoCompleteTextView autoCompleteTextViewAddress;... ArrayAdapter<String> adapter = new ArrayAdapter<String>(AvatarMainActivity.this, android 有一个简单的方法吗?
因为与一个与键入文本相同的元素的下拉列表看起来是多余的.

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

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.

当我们的适配器中只有一个匹配记录时,覆盖够的ToFilter函数会隐藏下拉列表

总结

以上是内存溢出为你收集整理的android – AutoCompleteTextView – 当我们在过滤后有一个完全匹配时如何禁用下拉列表?全部内容,希望文章能够帮你解决android – AutoCompleteTextView – 当我们在过滤后有一个完全匹配时如何禁用下拉列表?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1124611.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存