Android 如何检测按下自动填充?

问题描述 投票:0回答:2

我使用 AutofillManager https://developer.android.com/reference/android/view/autofill/AutofillManager 并且无法理解如何检测用户自己输入值或用户选择任何自动填充选项。 我尝试使用

editText.autofillValue
但它包含两种情况的值

有人知道如何解决吗?请帮助我!)

附注代码

我有请求自动填充的功能

fun allowAutoFill(view: View?) {
        if (view != null) {
            val afm = requireContext().getSystemService(AutofillManager::class.java)
            if (afm.isAutofillSupported && afm.isEnabled) {
                afm?.requestAutofill(view)
            }
        }
    }

之后我想知道用户输入了一些内容或从自动填充中选择了值。分析需要它。

private fun isAutoFillApplied() = binding?.editPassword?.autofillValue?.textValue?.isNotEmpty() == true

但是

binding?.editPassword?.autofillValue
如果用户输入某些内容并且选择自动填充选项,则包含值

android kotlin autofill android-autofill-manager
2个回答
0
投票

我找不到很酷的答案,所以我使用了糟糕的脏黑客。 我在屏幕上有两个编辑文本。如果用户选择任何自动填充选项,则此编辑文本会同时填充。因此,如果文本上的时间差变化不太大,我将

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
               }
           }

0
投票

老实说,我不知道为什么开发人员在 2023 年仍然要求人们重复密码两次。在过去 5 年里我从未输入过密码。但这不是重点。

AutofillNode 有一个名为 onFill 的参数。用它代替

© www.soinside.com 2019 - 2024. All rights reserved.