2016-11-20 101 views
0

有没有办法强制TextInputLayout错误消息采取单行?TextInputLayout错误消息大小

我试图把app:errorTextAppearance="@style/error_appearance"在TextInputLayout与包含error_appearance风格:

<item name="android:maxLines">1</item> 
<item name="android:ellipsize">end</item> 
<item name="android:inputType">text</item> 

,它不工作。但是改变颜色的作品。我不明白,mErrorView只是一个TextView,它应该工作。

TextInputLayout error message

+0

添加到XML此行 机器人:单线=“真” –

+0

@MalikAbuQaoud已经尝试过了,这是行不通的。和singleLine现已弃用 – andrei

+0

singleLIne仍在工作,它与我合作,好吧然后尝试将椭圆大小更改为字幕 –

回答

1

它是真实的错误文本只是一个TextView,但TextAppearance风格只影响文本本身,TextView不是如何勾画出来(除了大小的考虑,由于这些属性)。您可以使用errorTextAppearance设置错误文本的大小,字体,颜色等,但不能使用View来设置ellipsizemaxLines

由于错误只是TextView,我们可以自己设置所需的属性。我们可以遍历TextInputLayout的所有View s,但是这有点难以实施,并且不是很可靠,因为TextInputLayout可以容纳多于一个的TextView而不是它的EditText。我通常喜欢反思这样的事情。当setErrorEnabled()方法被调用与true创建

误差TextView,并且如果该方法被调用与false其字段是无效。与其试图从外部跟踪所有这些,子类TextInputLayout的子类将更容易,并且在创建时在错误TextView上设置所需的属性。请注意,在实例化过程中会自动调用setErrorEnabled(),因此不需要显式调用该方法来启用它。

例如:

public class CustomTextInputLayout extends TextInputLayout { 

    public CustomTextInputLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    public void setErrorEnabled(boolean enabled) { 
     super.setErrorEnabled(enabled); 

     if (!enabled) { 
      return; 
     } 

     try { 
      Field errorViewField = TextInputLayout.class.getDeclaredField("mErrorView"); 
      errorViewField.setAccessible(true); 
      TextView errorView = (TextView) errorViewField.get(this); 
      if (errorView != null) { 
       errorView.setMaxLines(1); 
       errorView.setEllipsize(TextUtils.TruncateAt.END); 
      } 
     } 
     catch (Exception e) { 
      // At least log what went wrong 
      e.printStackTrace(); 
     } 
    } 
} 

这是一个简易替换为TextInputLayout,你可以用它在你的布局,就像你的普通班。

<com.mycompany.myapp.CustomTextInputLayout 
    android:id="@+id/til" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.design.widget.TextInputEditText 
     android:id="@+id/edit_text" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</com.mycompany.myapp.CustomTextInputLayout> 

screenshot


为了完整起见,我会包括以下内容,其可以代替上述方法中使用,以找到第一TextView孩子TextInputLayout的,并设置所需的属性。在基本的,默认TextInputLayout,错误TextView应该是第一个找到。如果您需要额外的支票,您可以将找到的孩子的文本与TextInputLayout上的错误进行比较。

// Returns true if found 
private static boolean findErrorView(ViewGroup vg) { 
    final int count = vg.getChildCount(); 
    for (int i = 0; i < count; ++i) { 
     final View child = vg.getChildAt(i); 
     if(child instanceof ViewGroup) { 
      if(findErrorView((ViewGroup) child)) { 
       return true; 
      } 
     } 
     else if (child.getClass().equals(TextView.class)) { 
      setAttributes((TextView) child); 
      return true; 
     } 
    } 
    return false; 
} 

private static void setAttributes(TextView t) { 
    t.setMaxLines(1); 
    t.setEllipsize(TextUtils.TruncateAt.END); 
} 

TextInputLayoutViewGroup,所以你可以叫findErrorView()直接与您的这个实例,无论是内部的一个子类,或外部。尽管如此,请注意,错误TextView在错误被启用时会重新创建,并且在禁用时会被销毁。这些设置不会持续存在。

+1

老兄,你是一个天才..工作就像一个魅力..谢谢吨人。 – Rachit

0

您可以通过ID读取错误的TextView

TextView errorView = textInputLayout.findViewById(R.id.textinput_error); 
errorView.setMaxLines(1); 
errorView.setSingleLine(true);