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

问题描述:

我正在关注this guide以显示错误,并且this doc实施它。它工作正常,但不显示EditText下方的错误,我有他们浮动上方的警告图标。setError在弹出窗口中显示文本,而不是在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?

您应该通过TextInputLayout对象在那里。

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

然后

txtLayoutOBJ.setError("Invalid Format"); 
+1

哦,我应该使用的布局,不EditText上,酷 –

+0

@AlexeyStrakh是。使用'TextInputLayout'而不是'TextInputEditText' –

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