[用户输入时延迟TextInputLayout错误

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

我有一个简单的屏幕,其中有一个TextInputLayout和我有TextWatcher可以在用户输入时检查TextInputLayout输入是否与我的正则表达式匹配。 (因此没有用于检查的按钮,它在用户键入时进行检查)

我的问题是,我不想立即显示错误,因为用户仍在输入。我想设置一个1000ms的延迟,如果仍然错误,则显示错误。

由于没有按钮,单击按钮进行检查不适用于我的情况。在EditText失去焦点之后进行检查对我的情况不起作用,因为它永远不会失去焦点。

这里是代码。

    edt_input.addTextChangedListener(object : TextWatcher {

        override fun afterTextChanged(s: Editable?) {
            if (!s.isNullOrBlank() && s.matches(ARRAY_FORMAT_REGEX)) {
                til_input.error = null
                ..some code here
            } else {
                if (til_input.error == null) til_input.error = getString(R.string.invalid_input)
            }

        }

        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

    })

我尝试使用延迟,但不知道如何设置逻辑。

Timer("SettingUp", false).schedule(500) { 
   doSomething()
}
android android-edittext android-textinputlayout textwatcher
1个回答
0
投票

处理程序将像Timer一样完成工作,但更可靠。

edt_input.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable?) {
                Handler().postDelayed({
                    if (!s.isNullOrBlank() && s.matches(ARRAY_FORMAT_REGEX)) {
                        til_input.error = null
                        ..some code here
                    } else {
                        if (til_input.error == null) til_input.error = getString(R.string.invalid_input)
                    }
                },500)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

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