2016-11-20 7 views
0

TextInputLayout 오류 메시지를 한 줄로 표시하도록 할 수있는 방법이 있습니까?TextInputLayout 오류 메시지 크기

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

을 그리고 그것은 작동하지 않습니다

나는이 포함 된 error_appearance 스타일로 TextInputLayout에 app:errorTextAppearance="@style/error_appearance"을 배치했습니다. 그러나 색상 변경 작업. 나는 그것을 얻지 못한다. mErrorView는 단지 TextView 일 뿐이다.

(때문에 그 속성에 대한 크기 조정 고려 사항에서 떨어져)

TextInputLayout error message

+0

가 XML에 추가 ID로 오류 텍스트 뷰를 가져올 수있는이 선 안드로이드 : 만일 Singleline = "true"로 –

+0

@MalikAbuQaoud 이미 그것을 시도하고 그것이 작동하지 않습니다. 그리고 singleLine은 이제 비추천입니다 – andrei

+0

singleLIne 여전히 작동하고 그것은 나와 함께 작동합니다. 그러면 줄임표를 마키로 변경하려고합니다. –

답변

1

그것은 오류 텍스트 그냥 TextView입니다 사실,하지만 TextAppearance 스타일이 TextView이 그것을 낳는 방법 만 텍스트 자체에 영향을 미칩니다. errorTextAppearance을 사용하여 오류 텍스트의 크기, 활자체, 색상 등을 설정할 수 있지만 에는 ellipsize 또는 maxLines을 설정할 수 없습니다.

오류는 단지 TextView이므로 원하는 속성을 직접 설정할 수 있습니다. 우리는 TextInputLayout의 하위 객체 View을 반복 할 수 있지만, 다소 불편할 뿐더러 TextInputLayoutEditText이 아닌 하나 이상의 TextView을 보유 할 수 있기 때문에 신뢰성이 떨어집니다. 나는 보통 이와 같은 것들에 대한 반성을 선호한다. 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


, 나는 위의 방법 대신에 사용할 수있는 다음을 포함합니다하는 TextInputLayout의 첫 번째 TextView 아이를 찾을 수 원하는 속성을 설정하십시오. 기본 기본값 인 TextInputLayout에서 오류 TextView이 첫 번째로 표시되어야합니다. 추가 검사가 필요하면 발견 된 아동의 텍스트를 TextInputLayout의 오류로 설정된 텍스트와 비교할 수 있습니다. 당신은 단지 하나 서브 클래스 내부 또는 외부, 그 인스턴스와 직접 findErrorView()를 호출 할 수 있도록

// 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입니다. 그러나 오류가 활성화 될 때마다 TextView 오류가 새로 작성되고 오류가 발생하면 파기됩니다. 이러한 설정은이를 통해 유지되지 않습니다.

+1

야, 너 천재 야 .. 매력처럼 작동 .. 톤 남자에게 고마워. – Rachit

0

당신이

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