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

时间:2017-09-27 01:01:56

标签: android android-8.0-oreo

由于我们在Android O上安装了应用程序,因此推出了新功能,根据下图自动建议主页和工作位置。

enter image description here

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

2 个答案:

答案 0 :(得分:6)

显然在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);
    }

更新:找到另一种禁用AUTOFILL的方法 在XML中使用android:importantForAutofill="no" https://developer.android.com/guide/topics/text/testautofill.html#trigger_autofill_in_your_app

答案 1 :(得分: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