Android 如何检测自动填充已按下?
Android How can I detect autofill pressed?
我使用 AutofillManager https://developer.android.com/reference/android/view/autofill/AutofillManager 并且无法理解我如何检测是用户自己输入值还是用户 select 任何自动填充选项。
我尝试使用 editText.autofillValue
但它在两种情况下都包含值
有人知道我该如何解决吗?请帮助我!)
P.S。代码
我有请求自动填充的功能
fun allowAutoFill(view: View?) {
if (view != null) {
val afm = requireContext().getSystemService(AutofillManager::class.java)
if (afm.isAutofillSupported && afm.isEnabled) {
afm?.requestAutofill(view)
}
}
}
之后我想知道用户从自动填充中输入了一些东西或 select 值。分析需要它。
private fun isAutoFillApplied() = binding?.editPassword?.autofillValue?.textValue?.isNotEmpty() == true
但 binding?.editPassword?.autofillValue
包含值,如果用户输入内容并且如果用户 select 自动填充选项
我找不到很好的答案,所以我使用了糟糕的肮脏技巧。
我在屏幕上有两个编辑文本。如果用户 select 任何自动填充选项,此编辑文本将同时填充。因此,如果文本上的时间差异变化不大,我将 isAutofill
值填充为 true
。
这确实不是很好的解决方案,但我找不到另一个。
看起来像这样
var lastChangedTime = 0L
var isAutoFill = false
var lastRepeatPassword: String? = null
editPassword.addTextChangedListener { text ->
lastChangedTime = System.currentTimeMillis()
}
editRepeatPassword.addTextChangedListener { text ->
if (text.toString() != lastRepeatPassword) {
lastRepeatPassword = text.toString()
isAutoFill = (System.currentTimeMillis() - lastChangedTime) < 50
}
}
我使用 AutofillManager https://developer.android.com/reference/android/view/autofill/AutofillManager 并且无法理解我如何检测是用户自己输入值还是用户 select 任何自动填充选项。
我尝试使用 editText.autofillValue
但它在两种情况下都包含值
有人知道我该如何解决吗?请帮助我!)
P.S。代码
我有请求自动填充的功能
fun allowAutoFill(view: View?) {
if (view != null) {
val afm = requireContext().getSystemService(AutofillManager::class.java)
if (afm.isAutofillSupported && afm.isEnabled) {
afm?.requestAutofill(view)
}
}
}
之后我想知道用户从自动填充中输入了一些东西或 select 值。分析需要它。
private fun isAutoFillApplied() = binding?.editPassword?.autofillValue?.textValue?.isNotEmpty() == true
但 binding?.editPassword?.autofillValue
包含值,如果用户输入内容并且如果用户 select 自动填充选项
我找不到很好的答案,所以我使用了糟糕的肮脏技巧。
我在屏幕上有两个编辑文本。如果用户 select 任何自动填充选项,此编辑文本将同时填充。因此,如果文本上的时间差异变化不大,我将 isAutofill
值填充为 true
。
这确实不是很好的解决方案,但我找不到另一个。
看起来像这样
var lastChangedTime = 0L
var isAutoFill = false
var lastRepeatPassword: String? = null
editPassword.addTextChangedListener { text ->
lastChangedTime = System.currentTimeMillis()
}
editRepeatPassword.addTextChangedListener { text ->
if (text.toString() != lastRepeatPassword) {
lastRepeatPassword = text.toString()
isAutoFill = (System.currentTimeMillis() - lastChangedTime) < 50
}
}