Слушатели TextInputLayout не работают

1

Я работаю на экране регистрации, которые имеют несколько textInputLayout. В onResume() я добавляю слушателей textchange и focus ко всем текстовым входам. На первом экране все работает, все слушатели фокусировки и обмена текстами работают нормально. Но когда я иду к следующему экрану и снова возвращаюсь к тому же экрану, оба слушателя не работают. Я попытался 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
        }
    }

Когда я возвращаюсь к экрану, все входные данные присутствуют, но когда я пытаюсь изменить текст или фокус, слушатели не работают.

Теги:
kotlin
android-textinputlayout

1 ответ

0

Я смог решить эту проблему. Я точно не знаю причину, но когда я удалил kotlinX synthetic импорт 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)

}

Ещё вопросы

Сообщество Overcoder
Наверх
Меню