我目前正在使用AndroidStudio和kotlin
创建一个android应用。我已经达到创建一个“登录活动”的目的,并且在kotlin class
中的函数内有以下代码:
Username.error = null
val emailStr = Username.text.toString()
var cancel = false
var focusView: View? = null
if (TextUtils.isEmpty(emailStr)) {
Username.error = getString(R.string.error_field_required) //Username is the name of the editText in the layout xml file
focusView = Username
cancel = true
} else if (!isEmailValid(emailStr)) {
Username.error = getString(R.string.error_invalid_email)
focusView = Username
cancel = true
}
if (cancel) {
//If there was an error don't attempt login and focus the form field with an error.
focusView?.requestFocus()
}
该代码可以正常工作。直到我在文本区域内点击,出现的错误消息一直保持。我的问题是它涵盖了其他一些内容,因此我想让它说3秒后消失。我怎么做?
答案 0 :(得分:0)
我不太确定错误消息的显示方式,但是如果通过将其设置为null来删除错误消息,则可以在if(cancel)
if (cancel) {
focusView?.requestFocus()
launch() { //new thread so we don't lock the UI
delay(3000) //wait 3000 ms
Username.error = null
}
}
答案 1 :(得分:0)
使用androidx,您可以在三秒钟后删除错误:
//create a handler as class variable
val handler = Handler()
/in your method:
if (cancel) {
handler.postDelayed(3000){Username.error = null}
focusView?.requestFocus()
}
没有androidx:
if (cancel) {
handler.postDelayed({Username.error = null}, 3000)
focusView?.requestFocus()
}