显示并关闭键盘后,TextInputLayout错误文本未按预期显示

时间:2019-07-24 11:19:37

标签: android kotlin

我正在使用TextInputLayout来利用浮动提示和错误显示,当我按下“继续”按钮时,验证逻辑就会触发,如果我没有选择任何textInput,它会很好地工作,错误会正确显示,但是如果我选择一个textInput,关闭键盘,然后按继续按钮,textInput会按预期以红色突出显示,但不会显示错误文本。

似乎很奇怪的是,我在Acitvity上尝试了相同的代码,相同的布局,并且运行良好(我为此使用了Fragment),但我宁愿坚持使用Fragment。

我还检查了布局(未显示错误时),并且错误textView似乎在那里(在视图树中,您可以看到TextInputLayout内部的textView的宽度为0,文本设置为我设置的任何值)来

我的布局是:

<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">


        <TextView
            android:id="@+id/text_acc_details"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="24dp"
            android:text="@string/sign_up_details"
            android:textAppearance="@style/headerText"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:id="@+id/linear_acc_details"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="16dp"
            android:orientation="vertical"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/text_acc_details">

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/text_input_name"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:boxBackgroundMode="outline"
                app:errorEnabled="true"
                app:hintAnimationEnabled="true">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/text_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/sign_up_text_name"
                    android:inputType="text"
                    android:textColor="@color/textColour"
                    android:textSize="18sp" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/text_input_last_name"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:errorEnabled="true">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/text_last_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/sign_up_text_last_name"
                    android:inputType="text"
                    android:textColor="@color/textColour"
                    android:textSize="18sp" />
            </com.google.android.material.textfield.TextInputLayout>
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/text_input_email1"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                app:errorEnabled="true">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/text_email1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/sign_up_text_email1"
                    android:inputType="text"
                    android:textColor="@color/textColour"
                    android:textSize="18sp" />
            </com.google.android.material.textfield.TextInputLayout>

            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/text_input_email2"
                style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/text_email2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/sign_up_text_email2"
                    android:inputType="text"
                    android:textColor="@color/textColour"
                    android:textSize="18sp" />
            </com.google.android.material.textfield.TextInputLayout>
        </LinearLayout>

        <com.google.android.material.button.MaterialButton
            android:id="@+id/button_sign_up_continue"
            style="@style/PrimaryButton"
            android:layout_width="300dp"
            android:layout_marginBottom="23dp"
            android:enabled="true"
            android:text="@string/sign_up_button_continue"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.core.widget.NestedScrollView>

我的片段代码:

class SignUpFragment: Fragment() {

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        super.onCreateView(inflater, container, savedInstanceState)
        val rootView = inflater.inflate(R.layout.fragment_sign_up, container, false)
        return  rootView     
    }

    override fun onStart() {
        super.onStart()
        button_sign_up_continue.setOnClickListener {
            validateText()
        }
    }

    private fun validateText() {
        if (text_name.text.isNullOrEmpty()) {
            text_input_name.error = resources.getText(R.string.sign_up_error_text)
        } else {
            text_input_name.error = null
        }
        if (text_last_name.text.isNullOrEmpty()) {
            text_input_last_name.error = resources.getText(R.string.sign_up_error_text)
        } else {
            text_input_last_name.error = null
        }
        val isValidEmail = Patterns.EMAIL_ADDRESS.toRegex().matches(text_email1.text.toString())
        if (text_email1.text.isNullOrEmpty() || !isValidEmail) {
            text_input_email1.error = resources.getText(R.string.sign_up_invalid_email)
        } else {
            text_input_email1.error = null
        }
    }

gif显示了如何通过首先点击按钮来正常工作,但是如果我先选择textInput然后再点击按钮,则不会显示错误文本(通过检查布局检查器工具可以设置并显示错误文本,但是宽度出于某种原因是0)

enter image description here

2 个答案:

答案 0 :(得分:0)

  

将此方法放入onActivityCreated()而不是onStart()

button_sign_up_continue.setOnClickListener {
      validateText()
}

答案 1 :(得分:0)

在此问题上花费了大量时间后,我发现这似乎是Android的Material问题(至少在com.google.android.material:material:1.1.0库版本上),您可以找到问题追踪器在这里: https://issuetracker.google.com/issues/136435162

暂时,我发现了一种解决方法,可以通过创建TextInputLayout扩展并手动更新errorTextView宽度来解决errorTextView的宽度问题:

    val errorTextView = this.findViewById<TextView>(R.id.textinput_error)
    var params = errorTextView?.layoutParams
    params?.width = yourWidth
    errorTextView?.layoutParams = params

虽然不漂亮,但目前可以解决问题,希望它能对某人有所帮助