Turn off autosuggest for EditText?

2019-01-02 23:07发布

Is there a way to programmatically turn off that autosuggest list which pops up as you type in EditText?

11条回答
对你真心纯属浪费
2楼-- · 2019-01-03 00:03
EditText emailTxt=(EditText)findViewById(R.id.email);
emailTxt.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

This will help you.

查看更多
Viruses.
3楼-- · 2019-01-03 00:06
android:inputType="textNoSuggestions"  

also you'd better read this

查看更多
We Are One
4楼-- · 2019-01-03 00:07

You could simply use the EditText's setThreshold() method. Set the threshold to let's say 100 when you don't want to show predictions. If you want to re-activate showing predictions, set it back to a small int like 1 or 2 depending on your needs.

查看更多
Evening l夕情丶
5楼-- · 2019-01-03 00:10

I had the same question but I still wanted to set this option in my XML file so I did a little more research until I found it out myself.

Add this line into your EditText.

android:inputType="textFilter" 

Here is a Tip. Use this line if you want to be able to use the "enter" key.

android:inputType="textFilter|textMultiLine"
查看更多
Deceive 欺骗
6楼-- · 2019-01-03 00:10

The most reliable approach I have found to getting rid of autocomplete is to use

InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD 

on your EditText control. As charlie has reported in a different answer on this page,

android:inputType="textVisiblePassword"

is the XML version of this flag.

You can combine this flag with

InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS

I had been using InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS without InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD, which worked for most phones, but then I came across a Samsung phone for which I was still getting autocomplete.

Android programmatically disable autocomplete/autosuggest for EditText in emulator

which suggested using InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD.

I tried this (along with InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS) and it worked. You can see why even a phone that might not take the hint that you don't want autocomplete would have to allow it to be disabled for a password field. Short of holding our breath, this might be the best way to get what we want from such phones.

On some of my devices, the font was slightly changed by this flag - most noticeably to distinguish a zero (0) from an Oh (O) more clearly, which obviously would be important for displaying a password. But at least it worked, and the new font was not unattractive.

Even though the post on which I found this suggestion was old, the phone I tested on was very recent - a Samsung Galaxy Note II (SPH-L900) stock Android 4.1.2 from Sprint. The keyboard selected was "Samsung Keyboard," and apparently this was the default for this phone when the customer received it from Sprint. So this problem apparently has persisted over the years for at least some of the important Samsung line of phones.

For those of you who, like me, do not have a Samsung test device, this could be important information.

查看更多
登录 后发表回答