webentwicklung-frage-antwort-db.com.de

Android EditText Fehlermeldung Popup-Text wird nicht angezeigt

Ich habe ein Problem in meiner App, bei dem das Fehler-Popup in EditTexts angezeigt wird, der Text jedoch nicht sichtbar ist.

 EditText error message

Dies geschieht mit allen EditTexts in meiner App . Hier ist ein Beispiel für ein XML-Layout

Layout: 

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

Und die Stile:

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

Einstellungsfehler in der Aktivität mit firstnameTv.error = "This field can not be empty" (Kotlin)

6
Inn0vative1

Sie müssen style="@style/AppTheme.WhiteEditText" verwenden

Anstelle von Android:theme="@style/AppTheme.WhiteColorAccent"

Nun ist die Frage, warum style statt Android:theme verwendet werden muss.

Wenn Sie style verwenden, gilt dies nur für diese view.

und wenn Sie Android:theme verwenden, gilt view sowie alle children.

Lesen Sie mehr über What is the difference between style and Android:theme attributes?

BEISPIELCODE

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

stil

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

AUSGABE

 enter image description here

7
Nilesh Rathod

Sie müssen den Fehler in TextInputLayout festlegen, NICHT in TextInputEditText

firstnameTvLayout.setError("Error goes here");

Stellen Sie auch sicher, dass Sie das getan haben

firstnameTvLayout.setErrorEnabled(true);

Weitere Informationen finden Sie in der Dokumentation. https://developer.Android.com/reference/Android/support/design/widget/TextInputLayout

1
Irshad P I

Sie müssen style anstelle von Android:themeverwenden. Wenn Sie Android:theme für eine Ansichtsgruppe festlegen, verwenden die untergeordneten Elemente (in diesem Fall Fehler-Popup) denselben Stil.


Dein Code

<Android.support.design.widget.TextInputLayout
    ....
    Android:theme="@style/AppTheme.WhiteColorAccent"
    ....>

Ändern Sie es in

<Android.support.design.widget.TextInputLayout
    ....
    style="@style/AppTheme.WhiteColorAccent"
    ....>
0
Anees
textInputLayout.setErrorEnable(true);
0
Rajan Aghara