Android - AutoCompleteTextView not showing after setText is called

≡放荡痞女 提交于 2019-12-23 10:09:26

问题


I'm having a weird issue with AutoCompleteTextView.

I have a AutoCompleteTextView that shows suggestions of cities when typing in it. The list of cities is retrieved from a remote server via JSON. When I use the soft keyboard or the Mic Button on the soft keyboard, the suggestions work fine. AutoCompleteTextView does show the suggested cities.

But, I have a problem when I try to set the text using myAutoCompleteTextView.setText("Chi") , the auto complete does not show.. I have also tried myAutoCompleteTextView.append("Chi") but still no luck..

The adapter is there, its just that the suggestions don't show.

Any tips?

Thanks.


回答1:


Yes you are right there is a bug in AutocompleteTextview to show default suggestion using setText(""); method.

But you can achieve this by adding some more lines of code as below.

autoText.postDelayed(new Runnable() {
            @Override
            public void run() {
                autoText.showDropDown();
            }
        },500);
        autoText.setText("chi");
        autoText.setSelection(autoText.getText().length());



回答2:


Biraj Zalavadia's answer work, but you must write to "settext" in Runnable. Like this:

 mACTextViewEmail.postDelayed(new Runnable() {
            @Override
            public void run() {
                mACTextViewEmail.showDropDown();    
                mACTextViewEmail.setText("My text is here");
                mACTextViewEmail.setSelection(mACTextViewEmail.getText().length());
            }
        },500);



回答3:


It is due to filtering, No Need to any extra code for manage it, I found it in very easy and working way.

Google Dev. Reference link

autoText.setText("Default Value here",false)
autoText.setSelection(autoText.text.count()) // kotlin

as per documentation second parameter you can pass for filtering.

boolean: If false, no filtering will be performed as a result of this call.



来源:https://stackoverflow.com/questions/28184543/android-autocompletetextview-not-showing-after-settext-is-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!