1

我正在按照本指南显示错误,并按照本文档来实施它。它工作正常,但不是在 EditText 下方显示错误,而是让它们漂浮在上方并带有警告图标。

在此处输入图像描述

在此处输入图像描述

在此处输入图像描述

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


<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="30dp"
    app:errorEnabled="true"
    app:errorTextAppearance="@style/error">
        <android.support.design.widget.TextInputEditText
        android:id="@+id/txtPhoneNumber"
        android:textSize="24dp"
        android:singleLine="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Phone Number" />
</android.support.design.widget.TextInputLayout>


txtPhoneNumber.setError("Invalid Format");

如何使错误显示在 EditText 下方?

4

2 回答 2

2

您应该在那里传递TextInputLayout对象。

  <android.support.design.widget.TextInputLayout
   android:id="@+id/txtLayout">
   .........    
</android.support.design.widget.TextInputLayout>

然后

  txtLayoutOBJ.setError("Invalid Format");
于 2017-08-09T05:37:10.940 回答
1
TextInputLayout til = (TextInputLayout)findViewById(R.id.text_input_layout)
til.setError("Your Error");
于 2017-08-09T05:41:04.203 回答