Android: AutoCompleteTextView show suggestions when no text entered

后端 未结 14 1850
-上瘾入骨i
-上瘾入骨i 2020-11-27 11:20

I am using AutoCompleteTextView, when the user clicks on it, I want to show suggestions even if it has no text - but setThreshold(0) works exactly

相关标签:
14条回答
  • 2020-11-27 12:27

    Here is my class InstantAutoComplete. It's something between AutoCompleteTextView and Spinner.

    import android.content.Context;  
    import android.graphics.Rect;
    import android.util.AttributeSet;
    import android.widget.AutoCompleteTextView;
    
    public class InstantAutoComplete extends AutoCompleteTextView {
    
        public InstantAutoComplete(Context context) {
            super(context);
        }
    
        public InstantAutoComplete(Context arg0, AttributeSet arg1) {
            super(arg0, arg1);
        }
    
        public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
            super(arg0, arg1, arg2);
        }
    
        @Override
        public boolean enoughToFilter() {
            return true;
        }
    
        @Override
        protected void onFocusChanged(boolean focused, int direction,
                Rect previouslyFocusedRect) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
            if (focused && getAdapter() != null) {
                performFiltering(getText(), 0);
            }
        }
    
    }
    

    Use it in your xml like this:

    <your.namespace.InstantAutoComplete ... />
    
    0 讨论(0)
  • 2020-11-27 12:27

    The adapter does not perform filtering initially.
    When the filtering is not performed, the dropdown list is empty.
    so you might have to get the filtering going initially.

    To do so, you can invoke filter() after you finish adding the entries:

    adapter.add("a1");
    adapter.add("a2");
    adapter.add("a3");
    adapter.getFilter().filter(null);
    
    0 讨论(0)
提交回复
热议问题