Android EditText错误消息弹出文本未显示

前端之家收集整理的这篇文章主要介绍了Android EditText错误消息弹出文本未显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的应用程序中遇到问题,其中EditTexts上的错误弹出显示但文本不可见.
它看起来像这样:

我的应用程序中的所有EditTexts都会发生这种情况.
这是一个布局XML的示例

布局:

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout
    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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/gradient"
    android:fitsSystemWindows="true"
    tools:context=".ui.onboarding.profile.OnboardingUserProfileActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        app:srcCompat="@drawable/shapes_background" />

    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/form"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="80dp"
            android:orientation="vertical">

            <android.support.constraint.ConstraintLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="40dp"
                android:layout_marginBottom="20dp">

                <ImageView
                    android:id="@+id/profileImageView"
                    android:layout_width="160dp"
                    android:layout_height="160dp"
                    android:scaleType="fitXY"
                    android:src="@drawable/user_profile_placeholder"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"/>


                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/cameraImageButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:srcCompat="@android:drawable/ic_menu_camera" />
            </android.support.constraint.ConstraintLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/firstnameTvLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="20dp"
                android:layout_weight="0.33"
                android:textColorHint="@color/colorVeryLightGray"
                android:theme="@style/AppTheme.WhiteColorAccent"
                app:errorTextAppearance="@style/error_appearance">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/firstnameTv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_firstname"
                    android:inputType="textPersonName"
                    android:maxLines="1"
                    android:nextFocusDown="@id/lastnameTv"
                    android:nextFocusForward="@id/lastnameTv"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textColorHint="@color/colorWhite"
                    android:theme="@style/AppTheme.WhiteEditText"/>

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/lastnameTvLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="20dp"
                android:layout_weight="0.33"
                android:textColorHint="@color/colorVeryLightGray"
                android:theme="@style/AppTheme.WhiteColorAccent">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/lastnameTv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_lastname"
                    android:inputType="textPersonName"
                    android:maxLines="1"
                    android:nextFocusDown="@id/usernameTv"
                    android:nextFocusForward="@id/usernameTv"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textColorHint="@color/colorWhite"
                    android:theme="@style/AppTheme.WhiteEditText" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/usernameTvLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="0.33"
                android:textColorHint="@color/colorVeryLightGray"
                android:theme="@style/AppTheme.WhiteColorAccent">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/usernameTv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/prompt_username"
                    android:imeActionId="6"
                    android:imeActionLabel="@string/action_sign_in_short"
                    android:imeOptions="actionUnspecified"
                    android:maxLines="1"
                    android:nextFocusDown="@id/nextBtn"
                    android:nextFocusForward="@id/nextBtn"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textColorHint="@color/colorWhite"
                    android:theme="@style/AppTheme.WhiteEditText" />

            </android.support.design.widget.TextInputLayout>

        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/nextBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="16dp"
        android:text="@string/next"
        android:textStyle="bold"
        android:theme="@style/AppTheme.PrimaryButton"
        app:layout_anchor="@+id/form"
        app:layout_anchorGravity="bottom|center_horizontal" />

</android.support.design.widget.CoordinatorLayout>

和风格:

<style name="AppTheme.WhiteColorAccent">
    <item name="colorAccent">@color/colorWhite</item>
</style>
  <style name="AppTheme.WhiteEditText" parent="Widget.AppCompat.EditText">
    <item name="android:textColor">@color/colorWhite</item>
    <item name="colorControlNormal">@color/colorVeryLightGray</item>
    <item name="colorControlActivated">@color/colorWhite</item>
    <item name="colorControlHighlight">@color/colorWhite</item>
</style>

使用firstnameTv.error设置活动中的错误=“此字段不能为空”(Kotlin)

解决方法

你需要使用
风格= “@风格/ AppTheme.WhiteEditText”

而不是android:theme =“@ style / AppTheme.WhiteColorAccent”

Now question is why need to use style Instead of android:theme

使用样式时,它仅适用于该视图

当你使用android:theme时,它将应用视图以及它的所有子项.

阅读更多关于What is the difference between style and android:theme attributes?

示例代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scaleType="fitXY"
        android:visibility="gone" />

    <ProgressBar
        android:id="@+id/login_progress"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:visibility="gone" />

    <ScrollView
        android:id="@+id/form"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="20dp"
        android:fillViewport="true">

        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="80dp"
            android:orientation="vertical">

            <android.support.constraint.ConstraintLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:layout_marginTop="40dp"
                android:layout_marginBottom="20dp">

                <ImageView
                    android:id="@+id/profileImageView"
                    android:layout_width="160dp"
                    android:layout_height="160dp"
                    android:scaleType="fitXY"
                    android:src="@color/colorNavBar"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent" />


                <android.support.design.widget.FloatingActionButton
                    android:id="@+id/cameraImageButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="bottom|end"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintRight_toRightOf="parent"
                    app:srcCompat="@android:drawable/ic_menu_camera" />
            </android.support.constraint.ConstraintLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/firstnameTvLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="20dp"
                android:layout_weight="0.33"
                android:textColorHint="@color/colorVeryLightGray"
                style="@style/AppTheme.WhiteColorAccent"
                >

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/firstnameTv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="First Name"
                    android:inputType="textPersonName"
                    android:maxLines="1"
                    android:imeOptions="actionNext"
                    android:nextFocusDown="@id/lastnameTv"
                    android:nextFocusForward="@id/lastnameTv"
                    android:textColor="@android:color/white"
                    android:textColorHint="@color/colorWhite"
                    style="@style/AppTheme.WhiteEditText" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/lastnameTvLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="20dp"
                android:layout_weight="0.33"
                android:textColorHint="@color/colorVeryLightGray"
                style="@style/AppTheme.WhiteColorAccent">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/lastnameTv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Last Name"
                    android:inputType="textPersonName"
                    android:maxLines="1"
                    android:nextFocusDown="@id/usernameTv"
                    android:nextFocusForward="@id/usernameTv"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textColorHint="@color/colorWhite"
                    style="@style/AppTheme.WhiteEditText" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/usernameTvLayout"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_marginBottom="20dp"
                android:layout_weight="0.33"
                android:textColorHint="@color/colorVeryLightGray"
                style="@style/AppTheme.WhiteColorAccent">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/usernameTv"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="user Name"
                    android:imeActionId="6"
                    android:imeActionLabel="AB"
                    android:imeOptions="actionUnspecified"
                    android:maxLines="1"
                    android:nextFocusDown="@id/nextBtn"
                    android:nextFocusForward="@id/nextBtn"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textColorHint="@color/colorWhite"
                    style="@style/AppTheme.WhiteEditText" />

            </android.support.design.widget.TextInputLayout>

        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/nextBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="50dp"
        android:layout_marginRight="50dp"
        android:layout_marginTop="16dp"
        android:text="Next"
        android:textStyle="bold"
        app:layout_anchor="@+id/form"
        app:layout_anchorGravity="bottom|center_horizontal" />
</android.support.design.widget.CoordinatorLayout>

style

<style name="AppTheme.WhiteColorAccent">
    <item name="colorAccent">@color/colorWhite</item>
</style>

<style name="AppTheme.WhiteEditText" parent="Widget.AppCompat.EditText">
    <item name="android:textColor">@color/colorWhite</item>
    <item name="colorControlNormal">@color/colorVeryLightGray</item>
    <item name="colorControlActivated">@color/colorWhite</item>
    <item name="colorControlHighlight">@color/colorWhite</item>
</style>

OUTPUT

猜你在找的Android相关文章