问题
I have an activity that shows username UI, after entering that and tapping on continue button shows the enter password UI. On entering password and tapping on login button finishes the current activity and launches a new activity. On my device I selected Google autofill service, so on finish of the 1st activity I want "save for autofill?" dialog to show up, but it isn't. I prepared my app by adding autofillHints and nothing else in my activity. Should I add anything for the dialog to pop up?
回答1:
Despite following the docs and adding autofillHints
to my layouts, I was experiencing a similar behavior on the emulator where calls to AutofillManager.commit()
were not triggering the save UI when using the Google Autofill Service.
FWIW, I switched from the emulator to a physical device and the save UI started triggering properly even though I made no changes to my implementation. In both cases I was signed into a Google Account and in both cases Google's sample debug autofill service was triggering the save UI correctly.
I noticed you said "device," so maybe this doesn't apply to you, but it might be worth a shot to test on a physical device if you're using the emulator and there's seemingly no other explanation as to why it wouldn't be showing.
回答2:
I was having a similar issue to your case, kindly find the solution:
1- Make sure that you added the following permission to manifest:
<uses-permission
android:name="android.permission.BIND_AUTOFILL_SERVICE"
tools:ignore="ProtectedPermissions" />
2- Let's say in your login screen you have three EditText
(username, password, and captcha), So you MUST add android:importantForAutofill
for all the edit texts in the screen. If you missed adding it (for example for captcha cause no need to save it), Unfortunately, the autofill dialog will not show up.
3- To save username and password you should add for both username editText and password editText:
android:importantForAutofill="yes"
for username editText you should add :
android:autofillHints="username"
for password editText you should add :
android:autofillHints="password"
NOTE:
You should NOT use textNoSuggestions
for text input type for the password:
android:inputType="textPassword|textNoSuggestions"
Instead of that, you should use:
android:inputType="textPassword"
4- you should exclude the unwanted editText (Captcha) so as not be saved, So you should add to the captcha editText:
android:importantForAutofill="noExcludeDescendants"
5- Here is a snippet of the code:
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>
回答3:
Make sure Autofill with google is enabled in settings on your device as well:
System > Language & Input > Autofill Service > Choose google.
Depending on the device, it might be in a different nested settings page.
Also make sure to reads the docs on the API, if you want to create your custom autofill: https://developer.android.com/guide/topics/text/autofill
来源:https://stackoverflow.com/questions/56998420/save-for-autofill-dialog-not-showing-up