使用TextInputLayout的setError(“”)时遇到错误?

时间:2019-06-28 07:24:10

标签: android android-textinputlayout android-textinputedittext

下面是我的代码;

xml

  <android.support.design.widget.TextInputLayout
                android:id="@+id/emailTxtLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:theme="@style/TextLabel"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/button1">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/edt_editemail"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890@."
                    android:fontFamily="@font/poppins_light"
                    android:hint="@string/e_mail"
                    android:inputType="textEmailAddress"
                    android:maxLines="1"
                    android:singleLine="true"
                    android:textColor="@color/gray"
                    android:textColorHint="@color/gray"
                    android:textSize="@dimen/sp_16" />

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

样式

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

<style name="ErrorText" parent="TextAppearance.Design.Error">
    <item name="android:textSize">16sp</item>
    <item name="android:textColor">@color/darkorange</item>
</style>

代码

EditText edt_editemail;
TextInputLayout emailTxtLayout;

 edt_editemail = findViewById(R.id.edt_editemail);
 emailTxtLayout = findViewById(R.id.emailTxtLayout);

下面是我现在使用的依赖项;

implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'

当我使用下面的代码并单击“提交”按钮时,出现错误;

emailTxtLayout.setError(getString(R.string.valid_email_id_error));

错误在我下面;

  

java.lang.UnsupportedOperationException:无法解析索引4处的属性:TypedValue {t = 0x2 / d = 0x1010099 a = 1}           在android.content.res.TypedArray.getColor(TypedArray.java:462)           在android.widget.TextView(TextView.java:798)           在android.widget.TextView(TextView.java:732)           在android.support.v7.widget.AppCompatTextView(AppCompatTextView.java:87)           在android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:83)           在android.support.v7.widget.AppCompatTextView。(AppCompatTextView.java:79)           在android.support.design.widget.IndicatorViewController.setErrorEnabled(IndicatorViewController.java:422)           在android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:938)           在android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:1055)           在com.qltech.cunsumer.yupit.activities.RegisterActivity $ 4.onClick(RegisterActivity.java:307)           在android.view.View.performClick(View.java:5675)           在android.view.View $ PerformClick.run(View.java:22646)           在android.os.Handler.handleCallback(Handler.java:836)           在android.os.Handler.dispatchMessage(Handler.java:103)           在android.os.Looper.loop(Looper.java:203)           在android.app.ActivityThread.main(ActivityThread.java:6251)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1075)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)

Color.xml

<color name="colorPrimary">#FFFFFF</color>
<color name="colorPrimaryDark">#669b00</color>
<color name="colorAccent">#F05522</color>
<color name="darkorange">#F05522</color>
<color name="white">#FFFFFF</color>
<color name="black">#313131</color>
<color name="header_color">#eeeeee</color>
<color name="edit_text_line_color">#dddddd</color>
<color name="dark_black">#000000</color>
<color name="darkgreen">#99cc33</color>
<color name="green">#99cc33</color>
<color name="gray">#666666</color>
<color name="light_gray_b">#999999</color>
<color name="light_gray">#f5f5f5</color>
<color name="dark_gray">#e1e1e1</color>
<color name="transparent_color">#44000000</color>

2 个答案:

答案 0 :(得分:0)

像这样修改您的布局

<android.support.design.widget.TextInputLayout
            android:id="@+id/emailTxtLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:theme="@style/TextLabel"
            android:layout_centerVertical="true"
            android:layout_toRightOf="@+id/button1"
            app:errorEnabled="true">

您需要此属性来显示错误

app:errorEnabled="true"

然后打电话

emailTxtLayout.setError(getString(R.string.valid_email_id_error));

答案 1 :(得分:-1)

您可以尝试

  1. app:errorEnabled="true"
  2. setErrorTextAppearance
  3. 删除 android:theme="@style/TextLabel"

XML

<android.support.design.widget.TextInputLayout
    app:errorEnabled="true"
    app:errorTextAppearance="@style/ErrorStyle"
    >

然后

<style name="ErrorStyle" parent="TextAppearance.Design.Error">
        <item name="android:textSize">15sp</item>
        <item name="android:textColor">@color/darkorange</item>
    </style>
  

在此布局中是否启用了错误功能。   在通过设置错误消息之前启用此功能   setError(CharSequence),将表示此布局不会更改   显示错误时的大小。

仅供参考

UnsupportedOperationException (抛出),表明不支持请求的操作。

最后 Clean-Rebuild-Run