setError在弹出窗口中显示文本,而不是在EditText下方

时间:2017-08-09 05:32:34

标签: android textinputlayout android-textinputedittext

我正在关注this guide以显示错误,并this doc来实现错误。它工作正常,但不是在EditText下面显示错误,而是让它们在警告图标上方浮动。

enter image description here

enter image description here

enter image description here

<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下面?

2 个答案:

答案 0 :(得分:1)

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

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

然后

  txtLayoutOBJ.setError("Invalid Format");

答案 1 :(得分:1)

TextInputLayout til = (TextInputLayout)findViewById(R.id.text_input_layout)
til.setError("Your Error");