android 显示错误的TextInputLayout的setError

332nm8kg  于 2023-03-11  发布在  Android
关注(0)|答案(8)|浏览(112)

TextInputLayout工作正常,当尝试设置错误时,显示错误。

代码:

if (etFirstName.getText().length() == 0) {
            etFirstName.requestFocus();
            tvFirstName.setError("Please enter firstname");
        } else {
            tvFirstName.setError(null);
            tvFirstName.setErrorEnabled(false);
        }

检查日志:

java.lang.RuntimeException: Failed to resolve attribute at index 24
            at android.content.res.TypedArray.getColor(TypedArray.java:401)
            at android.widget.TextView.<init>(TextView.java:692)
            at android.widget.TextView.<init>(TextView.java:629)
            at android.widget.TextView.<init>(TextView.java:625)
            at android.widget.TextView.<init>(TextView.java:621)
            at android.support.design.widget.TextInputLayout.setErrorEnabled(TextInputLayout.java:297)
            at android.support.design.widget.TextInputLayout.setError(TextInputLayout.java:344)
8oomwypt

8oomwypt1#

为您的文本输入布局给予有效的样式(android:theme="@style/Theme.AppCompat”)

<android.support.design.widget.TextInputLayout
  android:id="@+id/testingInputLayout"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:theme="@style/Theme.AppCompat">

<EditText
   android:id="@+id/testingEditText"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:hint="@string/testText"
   android:inputType="textEmailAddress" />

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

您需要添加appCompat & support设计依赖项(如果没有)

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

在清单文件中添加AppCompat主题,

<application
 ...
android:theme="@style/Theme.AppCompat">
2j4z5cfb

2j4z5cfb2#

文本输入布局的Sytle主题:

<style name="HintInputTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">@color/input_floating_color</item>
<item name="android:textSize">22dp</item>
<item name="colorAccent">@color/input_floating_color</item>
</style>
<style name="InputTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#ff000000</item>
<item name="android:textColorHint">#ff000000</item>
<item name="android:textSize">24dp</item>
<item name="colorAccent">#ff000000</item>
</style>

<style name="ErrorTextAppearance" parent="TextAppearance.AppCompat">
<item name="android:textColor">#ffff0000</item>
<item name="android:textColorHint">#ffff0000</item>
<item name="android:textSize">18sp</item>
<item name="colorAccent">#ffff0000</item>
</style>

在布局中用作:

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/name_signup"
app:errorEnabled="true"
app:errorTextAppearance="@style/ErrorTextAppearance"
app:hintTextAppearance="@style/HintInputTextAppearance"
android:layout_marginLeft="@dimen/left_margin_login"
android:layout_marginRight="@dimen/right_margin_login"
android:layout_height="match_parent">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:textAppearance="@style/InputTextAppearance"
        android:layout_height="wrap_content"
        android:hint="Name" />
</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:id="@+id/email_signup"
app:errorEnabled="true"
app:errorTextAppearance="@style/ErrorTextAppearance"
app:hintTextAppearance="@style/HintInputTextAppearance"
android:layout_marginLeft="@dimen/left_margin_login"
android:layout_marginRight="@dimen/right_margin_login"
android:layout_height="match_parent">
    <android.support.design.widget.TextInputEditText
        android:layout_width="match_parent"
        android:textAppearance="@style/InputTextAppearance"
        android:layout_height="wrap_content"
        android:hint="Email" />
</android.support.design.widget.TextInputLayout>
368yc8dk

368yc8dk3#

主要片段.kt:

private fun onSubmit(){
   setErrorTextField(false)

}

 private fun setErrorTextField(error: Boolean){
         if(error){
            binding.textField.isErrorEnabled = true
            binding.textField.error = "Try again!"
         }else {
            binding.textField.isErrorEnabled = false
            binding.textInputEditText.text = null
         }
}

XML布局:

<com.google.android.material.textfield.TextInputLayout
            android:id="@+id/textField"
            style="@style/Widget.Unscramble.TextInputLayout.OutlinedBox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/default_margin"
            android:hint="@string/enter_your_word"
            app:errorIconDrawable="@drawable/ic_error"
            app:helperTextTextAppearance="@style/TextAppearance.MaterialComponents.Subtitle1"
            app:layout_constraintBottom_toTopOf="@+id/submit"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView_instructions">

            <com.google.android.material.textfield.TextInputEditText
                android:id="@+id/text_input_edit_text"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="textPersonName|textNoSuggestions"
                android:maxLines="1" />
        </com.google.android.material.textfield.TextInputLayout>
6qftjkof

6qftjkof4#

@Sevastyan的解决方案非常适合使用xml布局。
但是,如果我有一个TextInputLayout和一个TextInputEditText,它们都是我通过编程创建的,我这样做:

tilCode.setErrorEnabled(true);
tilCode.setErrorTextAppearance(R.style.error);

我会得到一个错误。但只是:

tilCode.setErrorTextAppearance(R.style.error);

对我有用。

1rhkuytd

1rhkuytd5#

使用这个它永远不需要主题.AppCompat

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/tv_metadata_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/tv_metadata_Details"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
7rtdyuoh

7rtdyuoh6#

使用文本工具

if(TextUtils.isEmpty(etFirstName.getText().toString()))
{
   etFirstName.setError("Filed is mandatory!");
   view = etFirstName;
}
b5buobof

b5buobof7#

尝试以下代码以显示TextInputLayout中的错误:

tvFirstName.setErrorEnabled(true);
 tvFirstName.setError("Please Enter firstName");
46qrfjad

46qrfjad8#

虽然我不完全理解为什么会出现这个问题,但它必须对Android如何根据其状态将颜色应用到视图做一些事情。
下面是我解决这个问题的方法:

1)定义自定义样式,该样式仅用于设置错误(或提示)的样式:

res/values/styles.xml中添加新样式节点:

<style name="error" parent="@android:style/TextAppearance">
        <item name="android:textColor">@color/colorAccent</item> <!--apply the color you wat here -->
        <item name="android:textSize">12dp</item>
    </style>

2)将样式应用到TextInputLayout

此外,请确保指定errorEnabled属性:

<android.support.design.widget.TextInputLayout
        android:id="@+id/input_email_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:errorEnabled="true"
        app:errorTextAppearance="@style/error"
    >
        <android.support.design.widget.TextInputEditText
            android:id="@+id/input_email"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:inputType="textEmailAddress"
                  android:hint="@string/hint_email"/>
    </android.support.design.widget.TextInputLayout>

相关问题