在 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
被接受的答案并不是一个解决方案,它并不能适用于所有情况。要完全禁用特定视图上的自动填充,您应该扩展它并覆盖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
}
}
android:inputType="textNoSuggestions"
这样的属性。 - Shobhit Puri