android自动填充服务
介绍 (Introduction)
Filling out forms is a time-consuming and tedious job. Most people get frustrated with long forms or forgotten passwords. To fill the gap between forums and user data, the Android team introduced the autofill framework, which is available in Android 8.0 (API level 26) and higher.
填写表格是一项耗时且乏味的工作。 大多数人对长格式或忘记密码感到沮丧。 为了填补论坛和用户数据之间的空白,Android团队引入了自动填充框架,该框架可在Android 8.0(API级别26)及更高版本中使用。
While using apps on your phone, you might’ve seen the login details filling up automatically by password-manager apps. The apps that fill other apps' views are called autofill services.
在手机上使用应用程序时,您可能会看到密码管理器应用程序自动填写了登录详细信息。 填充其他应用程序视图的应用程序称为自动填充服务。
By optimizing your app for autofill, you can provide more sophisticated user behavior, which helps them avoid input errors and save time, as they don’t need to write the same inputs multiple times.
通过优化应用程序的自动填充功能,您可以提供更复杂的用户行为,从而帮助他们避免输入错误并节省时间,因为他们不需要多次编写相同的输入。
组件 (Components)
When we see the autofill framework from the top level, we have mainly three components: the source of the data, the destination, and the mediator.
当我们从顶层看到自动填充框架时,主要有三个组件:数据源,目的地和中介器。
Autofill services: Apps such as password managers act as sources of data that stores the data that can be used across the apps
自动填充服务:密码管理器之类的应用程序充当数据源,用于存储可在各个应用程序中使用的数据
Autofill clients: These are the destination apps in which the data is to be filled
自动填充客户端:这些是要在其中填充数据的目标应用程序
Android system: This is the mediator that helps the above two components work together
Android系统:这是帮助上述两个组件协同工作的中介程序
自动填充提示 (Autofill Hints)
为什么我们需要实现自动填充提示? (Why do we need to implement autofill hints?)
As I said, the source of data is an autofill service — they try to determine the view using heuristics. So if they change the behavior, autofill also changes. To make sure the autofill service works correctly with the view, we need to provide autofill hints.
正如我所说的,数据源是自动填充服务-他们尝试使用启发式方法确定视图。 因此,如果他们改变了行为,自动填充也会改变。 为了确保自动填充服务在视图中正常工作,我们需要提供自动填充提示。
如何实现自动填充提示 (How to implement autofill hints)
We can either implement them in the XML layout or in the Kotlin class file.
我们可以在XML布局或Kotlin类文件中实现它们。
First, let’s see how to implement a simple hint in the XML using the attribute android:autofillHints
. The following code shows how to implement a password hint in the XML. Have a look:
首先,让我们看看如何使用android:autofillHints
属性在XML中实现简单提示。 以下代码显示了如何在XML中实现密码提示。 看一看:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="password" />
Now, we can implement the same in the Kotlin class file by using setAutofillHints
on the view. Have a look:
现在,我们可以通过在视图上使用setAutofillHints
在Kotlin类文件中实现相同的功能。 看一看:
edtPassword.setAutofillHints(View.AUTOFILL_HINT_PASSWORD)
The autofill framework won’t validate the hint constants. The following are some of the predefined hint constants:
自动填充框架不会验证提示常量。 以下是一些预定义的提示常量:
We can use
AUTOFILL_HINT_NEW_USERNAME
andAUTOFILL_HINT_NEW_PASSWORD
while creating an account or while logging in for the first time创建帐户或首次登录时,我们可以使用
AUTOFILL_HINT_NEW_USERNAME
和AUTOFILL_HINT_NEW_PASSWORD
We can use hints such as
AUTOFILL_HINT_CREDIT_CARD_NUMBER
,AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY
,AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH
,AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
, andAUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE
to autofill card-related details我们可以使用提示,例如
AUTOFILL_HINT_CREDIT_CARD_NUMBER
,AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY
,AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH
,AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR
,和AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE
详细信息We can also autofill address-related fields using the following constants:
AUTOFILL_HINT_POSTAL_ADDRESS_STREET_ADDRESS
,AUTOFILL_HINT_POSTAL_ADDRESS_EXTENDED_ADDRESS
,AUTOFILL_HINT_POSTAL_ADDRESS_LOCALITY
,AUTOFILL_HINT_POSTAL_ADDRESS_REGION
,AUTOFILL_HINT_POSTAL_ADDRESS_COUNTRY
,AUTOFILL_HINT_POSTAL_CODE
, andAUTOFILL_HINT_POSTAL_ADDRESS_EXTENDED_POSTAL_CODE
我们还可以使用自动填充以下常量地址相关字段:
AUTOFILL_HINT_POSTAL_ADDRESS_STREET_ADDRESS
,AUTOFILL_HINT_POSTAL_ADDRESS_EXTENDED_ADDRESS
,AUTOFILL_HINT_POSTAL_ADDRESS_LOCALITY
,AUTOFILL_HINT_POSTAL_ADDRESS_REGION
,AUTOFILL_HINT_POSTAL_ADDRESS_COUNTRY
,AUTOFILL_HINT_POSTAL_CODE
和AUTOFILL_HINT_POSTAL_ADDRESS_EXTENDED_POSTAL_CODE
自动填充的重要性级别 (Importance Level for Autofill)
We can tell the system whether the views in the hierarchy require autofill or not by using theandroid:importantForAutofill
attribute. By default, it uses IMPORTANT_FOR_AUTOFILL_AUTO
. Have a look:
我们可以使用android:importantForAutofill
属性来告诉系统层次结构中的视图是否需要自动填充。 默认情况下,它使用IMPORTANT_FOR_AUTOFILL_AUTO
。 看一看:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:importantForAutofill="no" />
We can also set the importance level in Kotlin classes by using the setImportantForAutofill
function. Have a look:
我们还可以使用setImportantForAutofill
函数在Kotlin类中设置重要性级别。 看一看:
tvSample.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
By default, it accepts five different values, such as:
默认情况下,它接受五个不同的值,例如:
auto
: This the default value, which lets the Android system use its heuristics to determine if the view is vital for autofillauto
:这是默认值,可让Android系统使用其启发式方法来确定视图是否对自动填充至关重要no
: This value represents that this view isn’t important for autofillno
:此值表示此视图对于自动填充并不重要- autofill 自动填充
yes
: This value represents that this view is vital for autofillyes
:此值表示此视图对于自动填充至关重要yesExcludeDescendants
: This view is important for autofill, but its children aren’t important for autofillyesExcludeDescendants
:此视图对于自动填充很重要,但其子级对于自动填充并不重要
强制执行自动填充请求 (Force the Autofill Request)
There will be times when you need to force the autofill request in response to a user’s actions. In this case, we can use the autofill menu item, which appears on the long press. The following code example shows how to force an autofill request:
有时,您需要强制执行自动填充请求以响应用户的操作。 在这种情况下,我们可以使用长按时出现的自动填充菜单项。 以下代码示例显示了如何强制执行自动填充请求:
fun HandlingEvent(view: v) {
val am = requireContext().getSystemService(AutofillManager::class.java)
am?.requestAutofill(v)
}
We can also cancel the autofill context using thecancel()
function. This can be handy if the user clears the text.
我们还可以使用cancel()
函数取消自动填充上下文。 如果用户清除文本,这将很方便。
奖金 (Bonus)
To learn more about Android advanced development, read the following articles:
要了解有关Android高级开发的更多信息,请阅读以下文章:
“How to Integrate Google Pay Into Your Existing Android App”
That is all for now — hope you learned something useful. Thanks for reading.
目前仅此而已-希望您学到了有用的东西。 谢谢阅读。
翻译自: https://medium.com/better-programming/exploring-the-autofill-framework-in-android-672221ccdc6a
android自动填充服务