Android:为什么setError消息导致View失真?

时间:2016-09-07 03:06:33

标签: android android-edittext android-textinputlayout

我有两个版本的应用我正在努力。如果用户尝试保存数据且行为空,则第一个版本具有在EditText UI行上设置的setError消息。 gradle dependencie使用版本24.0.0。

消息正确显示为:

enter image description here

该应用程序的第二个版本具有相同的setError代码,但View看起来失真,因为它看起来像View文档行已被View向下推,因此它不再直接位于“Do”文本和红色圆形感叹号之下点向下移动到错误消息的左侧。此版本使用gradle dependencie版本24.2.0。

enter image description here

关于可能导致这种情况的任何想法?

根据下面的Mike M的评论,我在TextInputLayout上使用了setError(),但这会产生一个完全不同的错误消息,显示在EditText行的开头,我不想使用它:

enter image description here

Activity.java

...
public void onClickSave(View v) {
    int stringTD = EditText.getText().toString().replace(" ", "").length();
    if (stringTD == 0) {
        EditText.requestFocus();            
        EditText.setError("Add a Do Item");

layout.xml

...
<android.support.design.widget.TextInputLayout
    android:id="@+id/TD_text_input_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textColorHint="@color/colorFlLabelFinal"
    app:hintTextAppearance="@style/FloatingLabel"  >

<com.EditText
    android:id="@+id/EditText"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="2dp"
    android:inputType="text|textCapSentences|textNoSuggestions"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF"
    android:textIsSelectable="true"
    android:textColorHighlight="@color/colorPrimary"
    android:paddingLeft="5dp"
    android:paddingStart="5dp"
    android:drawableStart="@drawable/24dp"
    android:drawableLeft="@drawable/24dp"
    android:drawablePadding="5dp"
    android:maxLines="1"
    android:maxLength="51"
    android:imeOptions="actionNext|flagNoExtractUi"
    android:nextFocusDown="@+id/DEditText" />

1 个答案:

答案 0 :(得分:1)

根据this,如果您在EditText内使用TextInputLayout,则此类行为会在版本24+之后发生。所以现在的解决方案就是单独使用EditText

尝试一下,看看它是否有效。