gpt4 book ai didi

android - TextInputLayout 监听器不工作

转载 作者:行者123 更新时间:2023-11-30 05:08:35 31 4
gpt4 key购买 nike

我正在处理注册屏幕,它有多个 textInputLayout。在 onResume() 中,我将文本更改和焦点监听器添加到所有文本输入布局。在打开屏幕上,它工作正常,所有焦点和文本更改监听器都工作正常。但是,当我转到下一个屏幕并再次返回同一屏幕时,两个听众都无法正常工作。我也尝试在 onViewCreated() 中使用 initiew() 但没有成功

private lateinit var customTextInputLayouts: MutableList<TextInputLayout>

override fun onResume() {
super.onResume()
initView()
}

private fun initView() {
customTextInputLayouts = mutableListOf(textInputLayoutURFirstName, textInputLayoutURLastName,
textInputLayoutUREmail, textInputLayoutURMobile, textInputLayoutURPassword)

customTextInputLayouts.forEach {
it.editText?.apply {
addTextChangedListener(generalTextWatcher)
onFocusChangeListener = generalFocusChangeListener
}
}
}

在下面的代码中,我在两个监听器中都进行了验证。

private val generalFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
if (!hasFocus) {
when {
R.id.textInputEditTextURFirstName == v.id && textInputLayoutURFirstName.editText?.text.toString().isValidName().first -> textInputLayoutURFirstName.setStyle(R.drawable.text_input_layout_background, 8)
R.id.textInputEditTextURLastName == v.id && textInputLayoutURLastName.editText?.text.toString().isValidName().first -> textInputLayoutURLastName.setStyle(R.drawable.text_input_layout_background, 8)
R.id.textInputEditTextUREmail == v.id && textInputLayoutUREmail.editText?.text.toString().isValidEmail().first -> {
mPresenter?.onEmailEntered(textInputLayoutUREmail.editText?.text.toString())
}
R.id.textInputEditTextURMobile == v.id && textInputLayoutURMobile.editText?.text.toString().isValidMobileNumber().first -> textInputLayoutURMobile.setStyle(R.drawable.text_input_layout_background, 4)
R.id.textInputEditTextURPassword == v.id && textInputLayoutURPassword.editText?.text.toString().isValidPassword().first -> textInputLayoutURPassword.setStyle(R.drawable.text_input_layout_background, 4)
}
}
checkRequireFieldAndEnableButton()
}

private val generalTextWatcher = object : TextWatcher {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
when {
textInputLayoutURFirstName.editText?.text?.hashCode() == s.hashCode() -> textInputLayoutURFirstName.error = textInputLayoutURFirstName.editText?.text.toString().isValidName().second
textInputLayoutURLastName.editText?.text?.hashCode() == s.hashCode() -> textInputLayoutURLastName.error = textInputLayoutURLastName.editText?.text.toString().isValidName().second
textInputLayoutUREmail.editText?.text?.hashCode() == s.hashCode() -> textInputLayoutUREmail.error = textInputLayoutUREmail.editText?.text.toString().isValidEmail().second
textInputLayoutURMobile.editText?.text?.hashCode() == s.hashCode() -> textInputLayoutURMobile.error = textInputLayoutURMobile.editText?.text.toString().isValidMobileNumber().second
textInputLayoutURPassword.editText?.text?.hashCode() == s.hashCode() -> textInputLayoutURPassword.error = textInputLayoutURPassword.editText?.text.toString().isValidPassword().second
}
checkRequireFieldAndEnableButton()
}

override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun afterTextChanged(s: Editable) {}
}

private fun checkRequireFieldAndEnableButton() {
if (textInputLayoutURFirstName.editText?.text.toString().isValidName().first && textInputLayoutURLastName.editText?.text.toString().isValidName().first && textInputLayoutUREmail.editText?.text.toString().isValidEmail().first
&& textInputLayoutURMobile.editText?.text.toString().isValidMobileNumber().first && textInputLayoutURPassword.editText?.text.toString().isValidPassword().first) {
buttonUserRegistrationSignUp.isEnabled = true
buttonUserRegistrationSignUp.alpha = 1.0f
} else {
buttonUserRegistrationSignUp.isEnabled = false
buttonUserRegistrationSignUp.alpha = 0.5f
}
}

当我返回屏幕时,所有输入数据都存在,但当我尝试更改文本或焦点监听器时,它不起作用。

最佳答案

我能够解决这个问题。我完全不知道原因,但是当我删除 kotlinX synthetic 导入并尝试以传统的 findViewById() 方式进行时。它的工作。

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

textInputFirstName = view.findViewById(R.id.textInputLayoutURFirstName)
textInputLastName = view.findViewById(R.id.textInputLayoutURLastName)
textInputPassword = view.findViewById(R.id.textInputLayoutURPassword)
textInputEmail = view.findViewById(R.id.textInputLayoutUREmail)
textInputMobile = view.findViewById(R.id.textInputLayoutURMobile)
buttonSingUp = view.findViewById(R.id.buttonUserRegistrationSignUp)

}

关于android - TextInputLayout 监听器不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54086512/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com