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

Android O auto location suggest feature (aka autofill), how to turn off

因为我们在 Android O 上有我们的应用程序,所以那里引入了一项新功能,它会根据下图自动建议家庭和工作地点。

这个叫什么?有没有办法从我们显示它的编辑文本中禁用它?

显然在 Android-Oreo 中,有这个新功能调用 AUTOFILL

https://developer.android.com/guide/topics/text/autofill.html,其中 By default, the view uses the IMPORTANT_FOR_AUTOFILL_AUTO mode, which lets Android use its heuristics to determine if the view is important for autofill

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

    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

接受的答案不是解决方案,它不适用于所有情况,要在特定视图上完全禁用自动填充,您应该扩展它并覆盖 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