10

我设计了一个自定义的 InputTextLayout,其中包含一个自定义的 Edittext。这个自定义的 Edittext 扩展了“AppCompatEdittext”。我的 MainActivity 也扩展了 AppCompatActivity。但我仍然无法使用 TextInputLayout 显示错误消息。以下是我来自各种文件的代码片段。

主要的.xml

<android.support.design.widget.TextInputLayout
            android:id="@+id/last_name_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/first_name_layout"
            android:layout_marginLeft="25dp"
            android:layout_marginRight="25dp"
            android:layout_marginTop="12dp"
            android:background="@drawable/grey_button_normal"
            android:hint="Last name"
            android:minHeight="48dp"
            android:paddingTop="3dp"
            android:theme="@style/TextLabel">

            <com.feebite.widgets.StyleableEditText
                android:id="@+id/lastname_edittext"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_gravity="center_vertical"
                android:background="@android:color/transparent"
                android:gravity="center_vertical"
                android:minHeight="48dp"
                android:nextFocusDown="@+id/email_edittext"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:singleLine="true"
                android:textColor="@color/darkish_grey"
                android:textColorHint="@color/darkish_grey"
                android:textCursorDrawable="@null"
                android:textSize="18sp" />

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

MainActivity.java

mLastNameLayout = (TextInputLayout) findViewById(R.id.last_name_layout);
 mLastNameLayout.setHint("Last name");
mLastNameLayout.setErrorEnabled(true);

来自 styles.xml 的自定义主题

<style name="TextLabel" parent="TextAppearance.AppCompat">
        <!-- Hint color and label color in FALSE state -->
        <item name="android:textColorHint">@color/darkish_grey</item>
        <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
        <item name="colorAccent">@color/darkish_grey</item>
        <item name="colorControlNormal">@color/darkish_grey</item>
        <item name="colorControlActivated">@color/dark_pink</item>
    </style>

并显示错误。

FATAL EXCEPTION: main
                                                             Process: com.feebite, PID: 18611
                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.feebite/com.feebite.userRegisteration.SignUpActivity}: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 24: TypedValue{t=0x3/d=0x45a "res/color/secondary_text_material_light.xml" a=1 r=0x106011e}
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                 at android.os.Looper.loop(Looper.java:148)
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                 at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117)
                                                              Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 24: TypedValue{t=0x3/d=0x45a "res/color/secondary_text_material_light.xml" a=1 r=0x106011e}
                                                                 at android.content.res.TypedArray.getColor(TypedArray.java:447)
                                                                 at android.content.res.XResources$XTypedArray.getColor(XResources.java:1033)
                                                                 at android.widget.TextView.<init>(TextView.java:738)
                                                                 at android.widget.TextView.<init>(TextView.java:671)
                                                                 at android.widget.TextView.<init>(TextView.java:667)
                                                                 at android.widget.TextView.<init>(TextView.java:663)
                                                                 at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:380)
                                                                 at com.feebite.userRegisteration.SignUpActivity.onCreate(SignUpActivity.java:59)
                                                                 at android.app.Activity.performCreate(Activity.java:6251)
                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                 at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                 at android.os.Looper.loop(Looper.java:148) 
                                                                 at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                                                                 at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:117) 
4

3 回答 3

33

尝试设置以下属性:

<item name="android:textColorHighlight">@color/Your color</item>
<item name="android:textColorLink">@color/Your color</item>
于 2016-03-21T19:38:18.760 回答
5

有完全相同的问题,没有一个答案得到解决。真正起作用的是删除这个:parent="TextAppearance.AppCompat"

于 2016-08-12T11:50:25.317 回答
0

您可以为 TextInputLayout 使用不同的属性,如

  • app:errorEnabled:切换错误可见性并在不应显示错误时使用空字符串。我们也可以使用 setError(null)。

  • app:errorTextAppearance:错误文本的文本外观。我们可以使用类似@style/TextInputLayout.ErrorText 的东西。

  • app:hintTextAppearance:提示文本的文本外观。我们可以使用类似@style/TextInputLayout.HintText 的东西。

在哪里:

<style name="TextInputLayout"/>

<style name="TextInputLayout.ErrorText">
  <item name="textColorError">#FFD50000</item>
</style>

<style name="TextInputLayout.HintText">
  <item name="android:textColor">?attr/colorPrimary</item>
  <item name="android:textSize">@dimen/text_tiny_size</item>
</style>

您正在寻找的是app:errorTextAppearance. 您还可以使用android:theme使容器的小 EditText(提示)继承您的应用程序主题配置。

于 2017-08-31T19:10:29.790 回答