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

标签 android kotlin

我正在使用 TextInputLayout 来利用 float 提示和错误显示,当我按下继续按钮时验证逻辑会触发,如果我不选择任何文本输入它工作正常,错误显示正确,但如果我选择一个 textInput,关闭键盘,然后按继续按钮,textInput 按预期以红色突出显示,但未显示错误文本。

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

我还检查了布局(未显示错误时)并且错误 textView 似乎在那里(在 View 树中,您可以看到 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>

我的 fragment 代码:

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

最佳答案

在这个问题上花了很多时间后,我发现这似乎是一个 Material Android 问题(至少在 com.google.android.material:material:1.1.0 库版本上),你可以找到问题跟踪器在这里: https://issuetracker.google.com/issues/136435162

目前我找到了解决 errorTextView 宽度问题的方法,方法是创建 TextInputLayout 扩展并手动更新 errorTextView 宽度:

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

它不是很漂亮,但它现在可以解决问题,希望它能帮助别人

关于android - 显示和关闭键盘后,TextInputLayout 错误文本未按预期显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57181698/

相关文章:

android - 以编程方式添加 EditText 缺失样式

android - 如何播放mp3数组中的音频[Android]

android - 如何在 Android Emulator 中编译和运行 Google Contacts App

android - 如何在 onTabChanged 中将样式应用于 TabHost 的选项卡?

android - 由于转换为使用 Gradle Kotlin DSL,无法解决依赖关系

kotlin - 空指针异常 : Testing DAO class that uses Ebean with Mockito and JUnit, Kotlin

android-studio - Kotlin中 Unresolved reference 错误(bottom_navigation.setOnNavigationItemSelectedListener)

android - Lollipop Activity 共享元素过渡与淡入淡出

Kotlin - 列表过滤函数的函数式编程递归实现

scala - 在Scala中调用Kotlin的重载方法