Android: AutoCompleteTextView show suggestions when no text entered

后端 未结 14 1918
-上瘾入骨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条回答
  •  Happy的楠姐
    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:

    
    

提交回复
热议问题