Android 关闭 EditText 的自动建议?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1959576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Turn off autosuggest for EditText?
提问by nikib3ro
Is there a way to programmatically turn off that autosuggest list which pops up as you type in EditText?
有没有办法以编程方式关闭在您输入 EditText 时弹出的自动建议列表?
采纳答案by glr
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.
我有同样的问题,但我仍然想在我的 XML 文件中设置这个选项,所以我做了更多的研究,直到我自己找到了它。
Add this line into your EditText.
将此行添加到您的 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"
回答by Mr_Hmp
android:inputType="textVisiblePassword"
works like a charm
奇迹般有效
回答by Enzokie
Ways to do it:
方法:
- Programatically
- 以编程方式
editText.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
- XML
- XML
android:inputType="textNoSuggestions"
If you already have some flags set, then:
如果您已经设置了一些标志,则:
- Programatically
- 以编程方式
inputType = if (disableSuggestions) {
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS or inputType
} else {
inputType and InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS.inv()
}
- XML
- XML
android:inputType="textNoSuggestions|textPassword"
回答by Carl
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,
在您的 EditText 控件上。正如查理在此页面上的不同答案中报道的那样,
android:inputType="textVisiblePassword"
is the XML version of this flag.
是此标志的 XML 版本。
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.
我一直在使用 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS 而没有 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD,这适用于大多数手机,但后来我遇到了三星手机,我仍然可以自动完成。
Android programmatically disable autocomplete/autosuggest for EditText in emulator
Android 以编程方式禁用模拟器中 EditText 的自动完成/自动建议
which suggested using InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD.
其中建议使用 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 haveto 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.
我尝试了这个(以及 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS)并且它起作用了。您可以理解为什么即使是可能不会提示您不希望自动完成功能的手机也必须允许将其禁用为密码字段。屏住呼吸,这可能是从此类手机中获得我们想要的东西的最佳方式。
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.
在我的一些设备上,这个标志稍微改变了字体 - 最明显的是更清楚地区分零 (0) 和哦 (O),这显然对于显示密码很重要。但至少它奏效了,而且新字体并不缺乏吸引力。
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.
尽管我发现这个建议的帖子很旧,但我测试的手机是最近的 - 来自 Sprint 的三星 Galaxy Note II (SPH-L900) 股票 Android 4.1.2。选择的键盘是“三星键盘”,显然这是客户从 Sprint 收到这款手机时的默认键盘。因此,对于至少一些重要的三星手机系列来说,这个问题多年来显然一直存在。
For those of you who, like me, do not have a Samsung test device, this could be important information.
对于像我一样没有三星测试设备的人来说,这可能是重要的信息。
回答by Mahesh Keshvala
For the android version 8.0 or above this code is not working and for the autocomplete textview also this code not working so i will suggest you to use below code for the Disable the auto suggestions in 8.0 or above android vesrion use this property of edittext android:importantForAutofill="no"
对于 android 8.0 或更高版本,此代码不起作用,对于自动完成文本视图,此代码也不起作用,因此我建议您使用以下代码禁用 8.0 或更高版本中的自动建议 android vesrion 使用 edittext android 的此属性 : importantForAutofill="no"
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:focusable="false"
android:imeOptions="actionDone"
android:maxLength="80"
android:maxLines="1"
android:textColor="@color/colorBlack"
android:textColorHint="@color/colorBlack"
android:importantForAutofill="no"
android:textSize="@dimen/_12sdp"
app:bFontsEdt="light" />
and for the AutoComplete textview like this use this Field to Disable Auto suggestion android:importantForAutofill="no":
对于像这样的 AutoComplete 文本视图,请使用此字段禁用自动建议android:importantForAutofill="no":
<AutoCompleteTextView
android:id="@+id/autocompleteEditTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/_3sdp"
android:layout_marginTop="@dimen/_5sdp"
android:background="@null"
android:imeOptions="actionDone"
android:singleLine="true"
android:text=""
android:textColor="@color/colorBlack"
android:textColorHint="@color/colorGrayDark"
android:importantForAutofill="no"
android:textSize="@dimen/_12sdp" />
回答by eagerprince
You can solve this problem by using this:
您可以使用以下方法解决此问题:
android:importantForAutofill="no"
Android O has the feature to support Auto-filling for fields,Just only use this in your xml, It will disable autofill hints
Android O 具有支持字段自动填充的功能,只需在您的 xml 中使用它,它将禁用自动填充提示
回答by Tariq
As this is still an issue, I'm going to post this answer. android:inputType="textVisiblePassword" works but it is undesirable, first because it's a misrepresentation of the input and second because it disables Gesture Typing!
由于这仍然是一个问题,我将发布此答案。android:inputType="textVisiblePassword" 可以工作,但它是不可取的,首先是因为它是对输入的误传,其次是因为它禁用了手势输入!
Instead I had to use BOTH flags textNoSuggestions and textFilter. Either one alone did not work.
相反,我不得不同时使用两个标志 textNoSuggestions 和 textFilter。任何一个单独都不起作用。
android:inputType="textCapSentences|textFilter|textNoSuggestions"
The textCapSentences was for other reasons. But this inputType kept the swipe typing and disabled the suggestion toolbar.
textCapSentences 是出于其他原因。但是这个 inputType 保留了滑动输入并禁用了建议工具栏。
回答by Ajji
I was getting this thing on samsung phones running 4.4.4. This solved my problem
我在运行 4.4.4 的三星手机上得到了这个东西。这解决了我的问题
android:inputType="text|textVisiblePassword|textNoSuggestions"
回答by nikib3ro
OK, the problem was - in Eclipse you don't get suggestion for flag named: textNoSuggestion
好的,问题是 - 在 Eclipse 中,您没有收到名为以下标志的建议:textNoSuggestion
And you can't set it in main.xml (where you design UI) because that attribute for inputType isn't recognized. So you can set it in code using int const:
并且您无法在 main.xml(您设计 UI 的地方)中设置它,因为 inputType 的该属性无法识别。所以你可以使用 int const 在代码中设置它:
EditText txtTypeIt = (EditText) this.findViewById(R.id.txtTypeIt);
txtTypeIt.setInputType(524288);
And thanks jasta00for helping me out figure answer for this one.
并感谢jasta00帮助我找出这个答案。