android - How to define Entry User name/Password/FirstName/LastName in Xamarin From so android will auto fill it?
问题描述
I know Android from certain version can detect the field is a field to enter User Name/ Password / Last Name / First Name?
But what I need to do in Xamarin Form for that to be happen?
解决方案
In API-27+ (Oreo+) it is preferable to use an AutoCompleteTextView
with the Autofill Framework but not absolutely needed.
Note: Xamarin.Forms
currently (up to 3.0.x
) does not implement the Android native AutoCompleteTextView
in any of the built-in Forms' controls but you can use just an Entry
control (implemented as a TextView
on Android).
On either your Android-based TextView
or AutoCompleteTextView
you should programmically call:
* SetAutofillHints
* ImportantForAutofill
Example:
usernameView.SetAutofillHints(new string[2] {"emailAddress", "userName"});
usernameView.ImportantForAutofill = ImportantForAutofill.Yes;
Note: In native Xamarin.Android
you can just set these attributes in the AXML layout.
re: https://codelabs.developers.google.com/codelabs/optimize-autofill/#0
Note: Review the Android docs for the basics on what the AutofillHints
strings should be set to...
In Xamarin.Forms
you can do this via a custom renderer or an effect:
Xamarin.Forms
Auto-fill Android Effect Example:
[assembly: ResolutionGroupName("EffectsAutoFill")]
[assembly: ExportEffect(typeof(AutoFillUserNameEffect), "AutoFillUserNameEffect")]
namespace Effects
{
public class AutoFillUserNameEffect : PlatformEffect
{
protected override void OnAttached()
{
var textView = (TextView)Control;
if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
{
textView.SetAutofillHints(new string[2] { "emailAddress", "userName" });
textView.ImportantForAutofill = ImportantForAutofill.Yes;
}
protected override void OnDetached()
{
}
}
}
Usage:
emailEntry.Effects.Add(Effect.Resolve("EffectsAutoFill.AutoFillUserNameEffect"));
Your Forms' app will now trigger whatevery auto-fill service the user have enabled on their device: Google, LastPass, etc.... (of course it is up to the user to enabled one or not...)
Output:
推荐阅读
- php - the_post_thumbnail $size 不起作用
- sql - 如何根据百分位数过滤表格,然后在 HQL 中随机抽样?
- javascript - Eslint:删除关键字功能
- hibernate - SpringBoot JPQL 查询 List NotIn List
- javascript - 获取所需的表单元素值返回未定义
- r - 以编程方式在 dplyr 中删除“group_by”字段
- spring - 如何使用 Spring Boot 和 @FeignClient 发送 Bearer 授权令牌
- xml - 用scala解析xml文件
- javascript - Billboard.js 可选 Y 网格线 - 标签背景的样式
- node.js - 错误:使用无服务器找不到模块 './drivers/node-mongodb-native/connection