0

我正在尝试在文本输入中实现这两种样式,但在应用errorTextAppearance样式时它不起作用我收到错误。

08-20 09:55:27.959 5081-5081/com.rayvatapps.ctvforme E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.rayvatapps.ctvforme, PID: 5081
java.lang.UnsupportedOperationException: Failed to resolve attribute at index 4: TypedValue{t=0x2/d=0x1010099 a=1}
    at android.content.res.TypedArray.getColor(TypedArray.java:469)
    at android.widget.TextView.<init>(TextView.java:892)
    at android.widget.TextView.<init>(TextView.java:818)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:87)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:83)
    at android.support.v7.widget.AppCompatTextView.<init>(AppCompatTextView.java:79)
    at android.support.design.widget.IndicatorViewController.setErrorEnabled(IndicatorViewController.java:422)
    at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:938)
    at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:1055)
    at com.rayvatapps.ctvforme.FormActivity.validateEmail(FormActivity.java:237)
    at com.rayvatapps.ctvforme.FormActivity.access$600(FormActivity.java:53)
    at com.rayvatapps.ctvforme.FormActivity$MyTextWatcher.afterTextChanged(FormActivity.java:326)
    at android.widget.TextView.sendAfterTextChanged(TextView.java:9375)
    at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:11917)
    at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:1262)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:574)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:504)
    at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:502)
    at android.view.inputmethod.BaseInputConnection.replaceText(BaseInputConnection.java:843)
    at android.view.inputmethod.BaseInputConnection.setComposingText(BaseInputConnection.java:616)
    at com.android.internal.view.IInputConnectionWrapper.executeMessage(IInputConnectionWrapper.java:396)
    at com.android.internal.view.IInputConnectionWrapper$MyHandler.handleMessage(IInputConnectionWrapper.java:85)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

XML

<android.support.design.widget.TextInputLayout
        android:id="@+id/tilname"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/TextLabel"
        app:errorTextAppearance="@style/error_appearance">

        <EditText
             android:id="@+id/edname"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:singleLine="true"
             android:hint="@string/full_name"
             android:textColorHint="@color/gray"
             android:textColor="@color/white"
             android:textSize="@dimen/_15sdp"
             android:inputType="" />

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

JAVA

 if (edName.getText().toString().trim().isEmpty()) {
            tilName.setError(getResources().getString(R.string.err_msg_name));
            requestFocus(edName);
            return false;
 } else 
 {
           tilName.setErrorEnabled(false);
 }

风格

<style name="error_appearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">@color/red</item>
    <item name="android:textSize">12sp</item>
</style>

<style name="TextLabel" parent="TextAppearance.AppCompat">
    <!-- Hint color and label color in FALSE state -->
    <item name="android:textColorHint">#4DFFFFFF</item>
    <item name="android:textSize">@dimen/_20sdp</item>

    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlNormal">#4DFFFFFF</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
    <item name="errorTextAppearance">@style/error_appearance</item>

</style>
4

1 回答 1

1

将 TextLabeltheme父级更改为 Widget.Design.TextInputLayout 后,它现在可以工作了

<style name="TextLabel" parent="Widget.Design.TextInputLayout">
    <item name="android:textColorHint">#4DFFFFFF</item>
    <item name="android:textSize">@dimen/_20sdp</item>
    <!-- Label color in TRUE state and bar color FALSE and TRUE State -->
    <item name="colorAccent">@color/colorAccent</item>
    <item name="colorControlNormal">#4DFFFFFF</item>
    <item name="colorControlActivated">@color/colorPrimary</item>
</style>
于 2018-08-20T06:40:27.503 回答