为应用程序禁用 Android O 自动填充服务



Android O具有支持自动填充字段的功能。有什么方法可以为特定应用程序禁用它。也就是说,我想强制我的应用程序不使用自动填充服务。

可能吗?

要阻止整个活动的自动填充,请在活动的 onCreate(( 中使用它:

getWindow()
.getDecorView()
.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);

有没有比这更好的方法?

目前没有直接的方法可以禁用整个应用程序的自动填充,因为自动填充功能是特定于视图的。

您仍然可以尝试这种方式,并在任何地方调用BaseActivity。

public class BaseActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
disableAutofill();
}
@TargetApi(Build.VERSION_CODES.O)
private void disableAutofill() { 
getWindow().getDecorView().setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS);
}
}

您也可以通过这种方式强制请求自动填充。

public void forceAutofill() {
AutofillManager afm = context.getSystemService(AutofillManager.class);
if (afm != null) {
afm.requestAutofill();
}
}

注意:目前自动填充功能仅在API 26 Android Oreo 8.0中可用

希望这有帮助!

我相信接受的答案是不正确的:

所以我有自己的类,它扩展android.support.v7.widget.AppCompatEditText,我所做的只是使用以下值覆盖以下方法:

@Override
public int getAutofillType() {
return AUTOFILL_TYPE_NONE;
}

没有其他解决方案有效,甚至Android:importantForAutofill="no"。

getAutofillType((来自 View 类,所以它也应该适用于所有其他类,例如 TextInputEditText!

我也遇到了这个问题。 事实证明,该问题是由于在嵌套在TextInputLayout中的EditText上设置提示文本引起的。

我做了一些挖掘,并在 26.0.0 Beta 2 发行说明中找到了这个金块。 Andorid 支持发行说明 2017 年 6 月

TextInputLayout 必须在 onProvideAutofillStructure(( 上设置提示

这促使我尝试在TextInputLayout上设置提示,而不是嵌套的EditText。

这为我解决了崩溃问题。 例:

<android.support.design.widget.TextInputLayout
android:id="@+id/textInputLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Some Hint Text"
android.support.design:hintAnimationEnabled="true"
android.support.design:hintEnabled="true"
android.support.design:layout_marginTop="16dp">
<android.support.design.widget.TextInputEditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.design.widget.TextInputLayout>

似乎是一个需要修复的错误:https://issuetracker.google.com/issues/67675432
同时,现在的解决方法是禁用整个项目的自动填充功能。 您可以在values-v26/styles.xml文件中添加以下样式,或者如果您为 EditText 视图使用特定样式,则可以编辑 BaseEditTextStyle。

<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
<item name="android:importantForAutofill">noExcludeDescendants</item>
</style>

values-v26/themes.xml文件中,您只需将项目editTextStyle添加到您在应用程序中使用的默认主题中,android:editTextStyle如下所示:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:editTextStyle">@style/App_EditTextStyle</item>
<item name="editTextStyle">@style/App_EditTextStyle</item>
</style>

通过这种方式,您可以将此更改应用于所有EditText,而无需更改布局文件或活动(稍后您可以在修复错误后轻松删除它(。

可能吗?

不是我知道的。当然,没有任何记录。

有没有比这更好的方法?

不是我知道的。

在 EditText 属性中添加android:importantForAutofill="no"这应该是一个临时修复程序,仅适用于 api 26+

创建自定义编辑文本样式并将android:importantForAutofill设置为no

<style name="EditTextStyleWithoutAutoFill" parent="Widget.AppCompat.EditText">
<item name="android:importantForAutofill">no</item>
</style>

然后在您的活动主题中为编辑文本样式设置此样式。

<item name="android:editTextStyle">@style/EditTextStyleWithoutAutoFill</item>

就我而言,我们的应用程序针对 SDK 版本 21,但较新的设备 (26+( 仍在弹出自动完成功能。如果应用程序在人与人之间共享的设备上运行,则这是一个很大的问题。仅使用android:importantForAutofill="no"对我不起作用。

我发现在我的情况下唯一有效的解决方案是:

<EditText
android:importantForAutofill="no"
tools:targetApi="o"
android:autofillHints="AUTOFILL_HINT_SMS_OTP" ...

我添加android:autofillHints="AUTOFILL_HINT_SMS_OTP"的原因是,如果您长时间按下 EditText,它仍然会显示自动填充。基本上,我告诉字段的自动填充,它正在等待永远不会发送的短信。有点黑客,我知道...

注意:如果尚未添加xmlns:tools="http://schemas.android.com/tools",则可能需要将其添加到您的架构中。

API 28+ 遇到同样的问题并禁用自动填充。对我来说,唯一的解决方案是禁用我的视图的长按。

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="false"
android:text="@={model.emailAdress}"/>

参考谷歌问题跟踪器,它已被修复。

这在安卓 8.1 上已修复

如果任何问题仍然存在,请在 Google 问题跟踪器上报告,他们将重新打开进行检查。

最新更新