网站建设与维护百科aspcms系统
android上的密码(其实不仅仅是密码,可以是用户名也可以是邮箱)自动填充,是需要考虑适配的。
 官方文档:https://developer.android.com/identity/autofill/autofill-optimize?hl=zh-cn
什么是自动填充
手机厂商一般会默认带一个。三星叫做pass,iqoo叫做密码保险箱,小米叫做智能密码管理。
 
 第三方的有,google框架,lastPass,bitwarden等。
 
 以上是自动填充服务。
 然后,就是需要我们app做的事情,适配自动填充。
 
适配
说起来,很简单。
 只需要申明
android:autofillHints="password" 
android:importantForAutofill="yes" 
 
但是发现这个事情,我们什么都不用做,默认就能支持的手机有pixel,小米等。但三星或者三方服务如lastpass可能存在一些问题。
其实官网上并没有说明有一种情况。坑就坑在,必须要有email或username框:
 必须要有Email框存在,才会被某些服务识别到密码框!
 因此, 你必须要有另外一个用户名的输入框比如:
android:autofillHints="emailAddress"  //username
android:importantForAutofill="yes" 
 
才能生效。
而且某些界面,比如你只存在一个输入密码的框,那么它将不能被弹出密码选择器。
 需要你隐藏一个邮箱输入框才能有效。
 比如:
 //不能是gone<includeandroid:id="@+id/hiddenEmail"layout="@layout/input_email_layout"android:visibility="invisible" android:layout_width="match_parent"android:layout_height="32dp"/>
 
反之,某些不想提示的:
 则
android:importantForAutofill="no" 
 
代码调用
给出2个函数:
/*** 在onCreate过程,添加代码。可以让它支持autoFill* @param autoFillHints 可以选如下:*     View.AUTOFILL_HINT_PASSWORD*     View.AUTOFILL_HINT_EMAIL_ADDRESS*     newPassword*/
fun EditText.makeAutoFill(autoFillHints:String) {setAutofillHints(autoFillHints)importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_YESimportantForAccessibility = View.IMPORTANT_FOR_AUTOFILL_YES
}//在onCreate过程,添加代码。可以让它不支持autoFill
fun EditText.makeNoAutoFill() {importantForAutofill = View.IMPORTANT_FOR_AUTOFILL_NOimportantForAccessibility = View.IMPORTANT_FOR_AUTOFILL_NO
}
