自动完成文本视图强制显示所有项目

20

我的应用中有一个场景,需要强制显示建议列表中的所有条目,无论用户键入了什么。我该如何做到这一点?

我尝试使用过滤器来实现,但对于我这样的初学者来说,过滤器太过复杂了。我尝试搜索过滤器的入门教程,但没有找到任何有用的内容。也许有一种更简单的方法可以强制显示所有建议项吗?

编辑: 基本上我的想法是,当用户输入不在列表中的内容时,显示所有可用选项。

我已经找到了检查ACTV是否正在显示的最佳方法,但在TextChangeEvent中,我将用户键入的文本与我的列表进行比较,如果没有找到任何元素,则显示完整列表。

public void onTextChanged(CharSequence s, int start, int before, int count)
         {                
           final EditText editText = (EditText) findViewById(R.id.vardsUserInput);
            String strValue = editText.getText().toString().toUpperCase();
            String temp;
            int Cc=0; //my count variable
            for(int i=0; i<vardi.length; i++)
            {
                temp = vardi[i].toUpperCase();
                if(temp.startsWith(strValue.toUpperCase()))
                {
                    Log.d("testing",vardi[i]);
                    Cc++;                                                   
                }
            }               
        if(Cc == 0)
        {
        //Show all the available options
    textView.showDropDown();                    
         }                  
}
12个回答

0
class InstantAutoComplete(context: Context?, attrs: AttributeSet?) : AutoCompleteTextView(context, attrs) {

override fun enoughToFilter(): Boolean {
    return true
}

override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
    super.onFocusChanged(focused, direction, previouslyFocusedRect)
    if (focused && filters != null) {
        performFiltering(text, 0)
    }
}

fun performFilter() {
    performFiltering(text, 0)
    showDropDown()
}

}

我需要在点击下拉按钮(imageview)后显示autocompletetextview中的所有数据。使用扩展AutoCompleteTextView类并添加此函数,它就像魔术一样工作。

0
关于上面的答案,请参考:https://dev59.com/NWgu5IYBdhLWcg3wfHI_#60223651 通过一些额外的修改,我成功得到了完美可行的解决方案。
  1. XML:在AutoCompleteTextView中添加以下代码。这样可以防止用户手动输入,并只能选择建议。

    android:inputType="none"
    android:focusableInTouchMode="false"
    android:cursorVisible="false"
    
  2. 代码:设置较高的阈值有助于始终显示所有建议。

    autotvLocations.setThreshold(100);  // 设置较高的阈值,以便每次都显示所有建议。
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接