从 Android 11 开始,键盘及其他输入法 (IME) 可以在建议栏或类似的容器中以内嵌方式显示自动填充建议,而不是系统在菜单中显示建议。由于这些自动填充建议可能包含隐私数据(如密码或信用卡信息),因此在用户选择某条建议之前,这些建议对 IME 隐藏。更新 IME 和自动填充服务(例如密码管理器),以使用此功能。如果 IME 或自动填充服务不支持内嵌自动填充功能,建议会显示在菜单中,就像在 Android 11 之前的版本中一样。
[null,null,["最后更新时间 (UTC):2025-08-21。"],[],[],null,["# Integrate autofill with IMEs and autofill services\n\nBeginning in Android 11, keyboards and other input-method editors\n(*IMEs* ) can display autofill suggestions inline, in a suggestion strip, or\nsomething similar instead of the system displaying suggestions in a menu. Since\nthese autofill suggestions can contain private data, such as passwords or\ncredit-card information, the suggestions are hidden from the IME until the user\nselects one. Update IMEs and autofill services, such as password managers, to\nmake use of this feature. If an IME or a autofill service doesn't support inline\nautofill, suggestions are shown in a menu, as in [versions earlier than\nAndroid 11](/guide/topics/text/autofill#guides).\n\nWorkflow\n--------\n\nIn this flow, *IME* means the current keyboard or other input editor, and\n*suggestion provider* means the appropriate provider of the autofill suggestion.\nDepending on the input field and the user's settings, the suggestion provider\nmight be the platform or an autofill service.\n\n1. The user focuses on an input field that triggers autofill, like a password or\n credit-card input field.\n\n2. The platform queries the current IME and the appropriate suggestion provider\n to see whether they support inline autofill. If either the IME or the\n suggestion provider doesn't support inline autofill, the suggestion is shown\n in a menu, as on Android 10 and lower.\n\n3. The platform asks the IME to provide a *suggestion request* . This suggestion\n request specifies the maximum number of suggestions to be displayed and also\n provides *presentation specs* for each suggestion. The presentation specs\n specify things like maximum size, text size, colors, and font data, letting\n the suggestion provider match the look and feel of the IME.\n\n4. The platform asks the suggestion provider to provide up to the requested\n number of suggestions. Each suggestion includes a callback to inflate a\n `View` containing the suggestion's UI.\n\n5. The platform informs the IME that suggestions are ready. The IME displays\n the suggestions by calling the callback method to inflate each suggestion's\n `View`. To protect the user's private information, the IME does *not* see\n what the suggestions are at this stage.\n\n6. If the user selects one of the suggestions, the IME is informed the same way\n as if the user picks a suggestion from a system menu.\n\nThe following sections describe how to configure your IME or autofill service to\nsupport inline autofill.\n\nConfigure IMEs to support inline autofill\n-----------------------------------------\n\nThis section describes how to configure your IME to support inline autofill. If\nyour IME doesn't support inline autofill, the platform defaults to showing\nautofill suggestions in a menu.\n\nYour IME must [set the `supportsInlinedSuggestions` attribute to\n`true`](/guide/topics/text/creating-input-method): \n\n \u003cinput-method\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:supportsInlineSuggestions=\"true\"/\u003e\n\nWhen the platform needs an autofill suggestion, it calls your IME's\n[`InputMethodService.onCreateInlineSuggestionsRequest()`](/reference/android/inputmethodservice/InputMethodService#onCreateInlineSuggestionsRequest(android.os.Bundle))\nmethod. You must implement this method. Return an\n[`InlineSuggestionsRequest`](/reference/android/view/inputmethod/InlineSuggestionsRequest)\nspecifying the following:\n\n- How many suggestions your IME wants.\n- An\n [`InlinePresentationSpec`](/reference/android/widget/inline/InlinePresentationSpec)\n for each suggestion, defining how the suggestion must be presented.\n\n | **Note:** If you provide fewer presentation specs than the number of suggestions requested, the last spec is used for all the excess suggestions. This means, for example, that if you provide only a single presentation spec, the suggestion provider uses that spec for all the suggestions.\n\nWhen the platform has suggestions, it calls your IME's\n[`onInlineSuggestionsResponse()`](/reference/android/inputmethodservice/InputMethodService#onInlineSuggestionsResponse(android.view.inputmethod.InlineSuggestionsResponse))\nmethod, passing an\n[`InlineSuggestionsResponse`](/reference/android/view/inputmethod/InlineSuggestionsResponse)\ncontaining the suggestions. You must implement this method. In your\nimplementation, call\n[`InlineSuggestionsResponse.getInlineSuggestions()`](/reference/android/view/inputmethod/InlineSuggestionsResponse#getInlineSuggestions())\nto get the list of suggestions, then inflate each suggestion by calling its\n[`InlineSuggestion.inflate()`](/reference/android/view/inputmethod/InlineSuggestion#inflate(android.content.Context,%20android.util.Size,%20java.util.concurrent.Executor,%20java.util.function.Consumer%3Candroid.widget.inline.InlineContentView%3E))\nmethod.\n\nConfigure autofill services to support inline autofill\n------------------------------------------------------\n\nThis section describes how to configure your autofill service to support inline\nautofill. If your app doesn't support inline autofill, the platform defaults to\nshowing its autofill suggestions in a menu.\n\nYour autofill service must [set the `supportsInlinedSuggestions` attribute to\n`true`](/guide/topics/text/autofill-services): \n\n \u003cautofill-service\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n android:supportsInlineSuggestions=\"true\"/\u003e\n\nWhen the IME needs autofill suggestions, the platform calls your autofill\nservice's\n[`onFillRequest()`](/reference/android/service/autofill/AutofillService#onFillRequest(android.service.autofill.FillRequest,%20android.os.CancellationSignal,%20android.service.autofill.FillCallback))\nmethod, just as it does in versions below Android 11. However, your\nservice must call the passed `FillRequest` object's\n[`getInlineSuggestionsRequest()`](/reference/android/service/autofill/FillRequest#getInlineSuggestionsRequest())\nmethod to get the `InlineSuggestionsRequest` created by the IME. The\n`InlineSuggestionsRequest` specifies how many inline suggestions are needed and\nhow each one must be presented. If the IME doesn't support inline suggestions,\nthe method returns `null`.\n\nYour autofill service creates\n[`InlinePresentation`](/reference/android/service/autofill/InlinePresentation)\nobjects, up to the maximum number requested in the `InlineSuggestionsRequest`.\nYour presentations must obey the size constraints specified by the\n`InlineSuggestionsRequest`. To return your suggestions to the IME, call\n[`Dataset.Builder.setValue()`](/reference/android/service/autofill/Dataset.Builder#setValue(android.view.autofill.AutofillId,%20android.view.autofill.AutofillValue,%20java.util.regex.Pattern,%20android.widget.RemoteViews,%20android.service.autofill.InlinePresentation))\nonce for each suggestion. Android 11 provides versions of\n`Dataset.Builder.setValue()` to support inline suggestions.\n| **Note:** Although the IME is supposed to use the suggestions your service provides, this function isn't guaranteed."]]