TextInputLayout侦听器无法正常工作

时间:2019-01-08 06:44:50

标签: android kotlin android-textinputlayout

我正在使用多个textInputLayout的注册屏幕。在onResume()中,我要向所有textinputlayout添加textchange和焦点侦听器。 在打开屏幕上,所有焦点和textchange侦听器都工作正常。但是,当我转到下一个屏幕并再次返回同一屏幕时,两个听众都无法正常工作。我也尝试过initiew()中的onViewCreated(),但没有运气

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

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

1 个答案:

答案 0 :(得分:0)

我能够解决此问题。我完全不知道原因,但是当我删除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)

}