Android O自动位置建议功能(又称自动填充),如何关闭

3

我们的Android O应用程序引入了一个新功能,它会自动建议家庭和工作位置,如下图所示。

enter image description here

这叫什么?有没有办法禁用我们的EditText显示它?


你能分享一下 EditText 的 XML 代码吗?或许可以在 EditText 上尝试使用 android:inputType="textNoSuggestions" 这样的属性。 - Shobhit Puri
1
"textNoSuggestion" 已被使用。但是自动填充功能正在忽略它。可能应该向 Google 提交一个错误报告。无论如何,我已经找到了一种编程控制它的方法,详见下面的答案。谢谢! - Elye
在https://issuetracker.google.com/issues/66975175上向谷歌提交了一个问题。 - Elye
2个回答

7

在 Android Oreo 中,似乎有这个名为自动填充(AUTOFILL)的新功能。

https://developer.android.com/guide/topics/text/autofill.html,其中 默认情况下,视图使用重要性为“IMPORTANT_FOR_AUTOFILL_AUTO”的模式,让 Android 使用其启发式算法来确定该视图是否对自动填充很重要。

因此,对于不打算填充的字段,只需将以下内容添加到您的视图中即可。

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        setImportantForAutofill(IMPORTANT_FOR_AUTOFILL_NO);
    }

更新:发现另一种禁用自动填充的方法。在XML中使用android:importantForAutofill="no"https://developer.android.com/guide/topics/text/testautofill.html#trigger_autofill_in_your_app


0

被接受的答案并不是一个解决方案,它并不能适用于所有情况。要完全禁用特定视图上的自动填充,您应该扩展它并覆盖getAutofillType()方法:

class TextInputEditTextNoAutofill : TextInputEditText {
    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    @RequiresApi(Build.VERSION_CODES.O)
    override fun getAutofillType(): Int {
        return View.AUTOFILL_TYPE_NONE
    }
}

这是 Kotlin 版本,但您可以理解要点。 示范存储库: https://github.com/BukT0p/AutofillBug

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接