有没有办法以编程方式关闭您在 EditText 中键入时弹出的自动建议列表?
17 回答
我有同样的问题,但我仍然想在我的 XML 文件中设置这个选项,所以我做了更多的研究,直到我自己发现它。
将此行添加到您的 EditText 中。
android:inputType="textFilter"
这是一个提示。如果您希望能够使用“回车”键,请使用此行。
android:inputType="textFilter|textMultiLine"
android:inputType="textNoSuggestions"
你也最好读读这个
android:inputType="textVisiblePassword"
奇迹般有效
方法:
- 以编程方式
editText.inputType = InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
- XML
android:inputType="textNoSuggestions"
如果您已经设置了一些标志,那么:
- 以编程方式
inputType = if (disableSuggestions) {
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS or inputType
} else {
inputType and InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS.inv()
}
- XML
android:inputType="textNoSuggestions|textPassword"
对于 android 版本 8.0 或更高版本,此代码不起作用,对于自动完成 textview,此代码也不起作用,因此我建议您使用以下代码在 8.0 或更高版本 android vesrion 中使用以下代码来禁用自动建议,使用 edittext android 的此属性:重要ForAutofill =“否”
<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" />
对于这样的自动完成文本视图,请使用此字段来禁用自动建议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" />
您可以使用以下方法解决此问题:
android:importantForAutofill="no"
Android O 具有支持自动填充字段的功能,只需在您的 xml 中使用它,它将禁用自动填充提示
我发现摆脱自动完成的最可靠方法是使用
InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
在您的 EditText 控件上。正如查理在此页面上的不同答案中所报道的那样,
android:inputType="textVisiblePassword"
是此标志的 XML 版本。
您可以将此标志与
InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS
我一直在使用不带 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD 的 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS,这适用于大多数手机,但后来我遇到了一部三星手机,我仍然可以自动完成。
Android以编程方式禁用模拟器中EditText的自动完成/自动建议
建议使用 InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD。
我试过这个(连同 InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS),它奏效了。您可以看到为什么即使是可能没有提示您不希望自动完成的手机也必须允许它在密码字段中被禁用。没有屏住呼吸,这可能是从此类手机中获得我们想要的东西的最佳方式。
在我的一些设备上,这个标志稍微改变了字体——最明显的是为了更清楚地区分零 (0) 和哦 (O),这显然对于显示密码很重要。但至少它起作用了,而且新字体也不是没有吸引力。
尽管我发现这个建议的帖子很旧,但我测试的手机是最新的——Sprint 的三星 Galaxy Note II (SPH-L900) 库存 Android 4.1.2。选择的键盘是“三星键盘”,显然这是客户从 Sprint 收到这款手机时的默认设置。因此,对于至少一些重要的三星手机系列,这个问题显然多年来一直存在。
对于像我一样没有三星测试设备的人来说,这可能是重要信息。
我在运行 4.4.4 的三星手机上得到了这个东西。这解决了我的问题
android:inputType="text|textVisiblePassword|textNoSuggestions"
由于这仍然是一个问题,我将发布此答案。android:inputType="textVisiblePassword" 有效,但它是不可取的,首先是因为它是对输入的误传,其次是因为它禁用了手势输入!
相反,我不得不同时使用标志 textNoSuggestions 和 textFilter。单独一个都行不通。
android:inputType="textCapSentences|textFilter|textNoSuggestions"
textCapSentences 是出于其他原因。但是这个 inputType 保留了滑动输入并禁用了建议工具栏。
这对我有用
android:importantForAutofill="no"
android:inputType="none|textNoSuggestions"
好的,问题是 - 在 Eclipse 中,您没有得到名为:textNoSuggestion 的标志的建议
而且您无法在 main.xml (您设计 UI 的地方)中设置它,因为 inputType 的该属性无法识别。所以你可以使用 int const 在代码中设置它:
EditText txtTypeIt = (EditText) this.findViewById(R.id.txtTypeIt);
txtTypeIt.setInputType(524288);
感谢jasta00帮我找出答案。
// 100% correct & tested
txtKey = (EditText) view.findViewById(R.id.txtKey);
txtKey.setPrivateImeOptions("nm");
txtKey.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
txtKey.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
}
android:inputType="text|textNoSuggestions"
EditText emailTxt=(EditText)findViewById(R.id.email);
emailTxt.setInputType(android.text.InputType.TYPE_CLASS_TEXT | android.text.InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
这将对您有所帮助。
您可以简单地使用 EditText 的setThreshold()
方法。当您不想显示预测时,将阈值设置为 100。如果您想重新激活显示预测,请根据您的需要将其设置回像 1 或 2 这样的小整数。
如果您不想要建议,您可以使用“无”
android:inputType="none|numberDecimal"
使用 TextWatcher 为自动建议找到一个好的解决方案。建议将显示给用户,但用户将无法接受它们。
/*...*/
editText.addTextChangedListener(textWatcher);
/*...*/
}
private final TextWatcher textWatcher = new TextWatcher() {
long timestamp = 0L;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (count - before > 1 && timestamp < System.currentTimeMillis()) { // disable suggestions click
String str = s.toString();
str = str.substring(0, start) + str.substring(start + count - 1, str.length() - 1);
timestamp = System.currentTimeMillis() + 20;
inputText.setText(str);
inputText.setSelection(inputText.getText().length());
}
}
@Override
public void afterTextChanged(Editable s) {
}
};
当您不需要长按复制/粘贴功能时,这也很有效