Android: AutoCompleteTextView show suggestions when no text entered

后端 未结 14 1847
-上瘾入骨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:12

    Destil's answer above almost works, but has one subtle bug. When the user first gives focus to the field it works, however if they leave and then return to the field it will not show the drop down because the value of mPopupCanBeUpdated will still be false from when it was hidden. The fix is to change the onFocusChanged method to:

    @Override
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            if (getText().toString().length() == 0) {
                // We want to trigger the drop down, replace the text.
                setText("");
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 12:14

    Just call this method on touch or click event of autoCompleteTextView or where you want.

    autoCompleteTextView.showDropDown()
    
    0 讨论(0)
  • 2020-11-27 12:17

    Destil's code works just great when there is only one InstantAutoComplete object. It didn't work with two though - no idea why. But when I put showDropDown() (just like CommonsWare advised) into onFocusChanged() like this:

    @Override
    protected void onFocusChanged(boolean focused, int direction,
            Rect previouslyFocusedRect) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect);
        if (focused) {
            performFiltering(getText(), 0);
            showDropDown();
        }
    }
    

    it solved the problem.

    It is just the two answers properly combined, but I hope it may save somebody some time.

    0 讨论(0)
  • 2020-11-27 12:18

    This worked for me, pseudo code:

        public class CustomAutoCompleteTextView extends AutoCompleteTextView {
        public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean enoughToFilter() {
            return true;
        }
    
        @Override
        protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
            if (focused) {
                performFiltering(getText(), 0);
            }
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            this.showDropDown();
            return super.onTouchEvent(event);
        }
    }
    

    0 讨论(0)
  • 2020-11-27 12:18

    on FocusChangeListener, check

    if (hasFocus) {
                tvAutoComplete.setText(" ")
    

    in your filter, just trim this value:

    filter { it.contains(constraint.trim(), true) }
    

    and it will show all suggestion when you focus on this view.

    0 讨论(0)
  • 2020-11-27 12:25

    Just paste this to your onCreate Method in Java

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
                this, android.R.layout.simple_spinner_dropdown_item,
                getResources().getStringArray(R.array.Loc_names));
    
        textView1 =(AutoCompleteTextView) findViewById(R.id.acT1);
        textView1.setAdapter(arrayAdapter);
    
        textView1.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(final View arg0) {
                textView1.setMaxLines(5);
                textView1.showDropDown();
    
            }
        });
    

    And this to your Xml file...

    <AutoCompleteTextView
                android:layout_width="200dp"
                android:layout_height="30dp"
                android:hint="@string/select_location"
                android:id="@+id/acT1"
                android:textAlignment="center"/>
    

    And create an Array in string.xml under Values...

    <string-array name="Loc_names">
    
            <item>Pakistan</item>
            <item>Germany</item>
            <item>Russia/NCR</item>
            <item>China</item>
            <item>India</item>
            <item>Sweden</item>
            <item>Australia</item>
        </string-array>
    

    And you are good to go.

    0 讨论(0)
提交回复
热议问题