自动填充对话框不显示的保存问题

7

我有一个显示用户名UI的活动,在输入用户名并点击继续按钮后会显示输入密码的UI。在输入密码并点击登录按钮后,当前活动将结束并启动新的活动。 在我的设备上,我选择了Google自动填充服务,因此在第一个活动完成时,我希望"保存到自动填充?"对话框出现,但实际上没有出现。我已经在我的活动中添加了autofillHints,除此之外还需要添加其他内容才能弹出对话框吗?


1
同样的问题,你解决了吗? - Papps
1
你解决了吗?@ManuleK - Maulik Dodia
3个回答

3

我遇到了类似的问题,以下是解决方法:

1- 确保在清单文件中添加了以下权限:

    <uses-permission
    android:name="android.permission.BIND_AUTOFILL_SERVICE"
    tools:ignore="ProtectedPermissions" />

2- 假设在您的登录界面中有三个EditText(用户名、密码和验证码),因此您必须为屏幕上的所有编辑文本添加android:importantForAutofill。如果您忘记添加它(例如对于验证码,因为不需要保存它),很遗憾,自动填充对话框将不会显示出来。
3- 要保存用户名和密码,您应该为用户名EditText和密码EditText都添加:
android:importantForAutofill="yes"

对于用户名(username)的编辑框(editText),您应该添加以下内容:

 android:autofillHints="username"

对于密码 editText,您应该添加:

 android:autofillHints="password"

注意: 在密码输入框中,不应使用textNoSuggestions文本输入类型:

 android:inputType="textPassword|textNoSuggestions"

你应该使用以下方法:

 android:inputType="textPassword"

4- 你应该排除不需要的editText(验证码)以免被保存,因此你应该在验证码editText中添加:

 android:importantForAutofill="noExcludeDescendants"  

5- 这里是代码片段:

com.google.android.material.textfield.TextInputLayout
            android:id="@+id/usernameTextInputLayout"
            style="@style/TextInputStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_16"
            android:layout_marginEnd="@dimen/margin_16"
            android:layout_marginBottom="@dimen/margin_16"
            app:endIconDrawable="@drawable/ic_username"
            app:endIconMode="custom"
            app:endIconTint="?attr/theme_primary">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/usernameEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:autofillHints="username"
                android:hint="@string/hint_username"
                android:imeOptions="actionNext"
                android:importantForAutofill="yes"
                android:inputType="text"
                android:maxLines="1"
                android:textAlignment="viewStart" />

        </com.google.android.material.textfield.TextInputLayout>

        <com.google.android.material.textfield.TextInputLayout
            android:id="@+id/passwordTextInputLayout"
            style="@style/TextInputStyle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginStart="@dimen/margin_16"
            android:layout_marginEnd="@dimen/margin_16"
            android:layout_marginBottom="@dimen/margin_16"
            app:endIconDrawable="@drawable/ic_password"
            app:endIconMode="custom"
            app:endIconTint="?attr/theme_primary">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/passwordEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:autofillHints="password"
                android:hint="@string/hint_password"
                android:importantForAutofill="yes"
                android:inputType="textPassword"
                android:textAlignment="viewStart" />

        </com.google.android.material.textfield.TextInputLayout>

 <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/captchaTextInputLayout"
                style="@style/TextInputStyle"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginEnd="@dimen/margin_16"
                android:layout_marginBottom="@dimen/margin_16"
                android:importantForAutofill="noExcludeDescendants"
                app:boxCornerRadiusTopEnd="0dp"
                app:boxCornerRadiusTopStart="0dp">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/captchaEditText"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/hint_captcha_input"
                    android:imeOptions="actionNext"
                    android:importantForAutofill="noExcludeDescendants"
                    android:inputType="numberDecimal"
                    android:maxLength="@integer/otp_input_length"
                    android:maxLines="1"
                    android:textAlignment="viewStart" />

            </com.google.android.material.textfield.TextInputLayout>

不需要权限,只授予系统应用程序。 - Homayoon Ahmadi

1
尽管我遵循文档并将autofillHints添加到我的布局中,但在使用Google Autofill服务时,在模拟器上调用AutofillManager.commit()未触发保存UI的类似行为。

顺便说一下,我从模拟器切换到物理设备,即使我没有更改实现,保存UI也开始正确触发。 在两种情况下,我都登录了Google帐户,并且在两种情况下,Google的示例调试自动填充服务都可以正确触发保存UI。

我注意到你说“设备”,所以也许这不适用于您,但如果您正在使用模拟器且似乎没有其他解释为什么它不会显示,则值得在物理设备上测试一下。


0

请确保在您的设备设置中启用了 Google 自动填充功能: 系统 > 语言和输入 > 自动填充服务 > 选择 Google。

根据设备不同,可能会在不同的嵌套设置页面中找到。

如果您想创建自定义自动填充,请确保阅读 API 文档: https://developer.android.com/guide/topics/text/autofill


1
我在我的问题中已经提到我选择了Google Autofill服务,我按照文档中给出的所有步骤进行了操作,在xml中为我的视图添加了提示,但仍然没有看到任何对话框。 - ManuleK

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