2016-09-06 1 views
1

TextInputLayout으로 싸여진 EditText을 사용하고 있습니다. 입력 된 텍스트 자체에 대해 textSizeEditText으로 설정하면 프로그래밍 방식으로 작동합니다. 그러나 입력이 비어있을 때 표시되는 EditText의 힌트 텍스트에는 적용되지 않습니다.TextInputLayout 내에서 EditText의 힌트 크기를 프로그래밍 방식으로 설정하십시오.

여기에 뭔가가 누락되었거나 버그입니까? 후자의 경우 해결 방법이 있습니까? 사용자 정의 View

TextInputLayout :

<android.support.design.widget.TextInputLayout 
    android:id="@+id/password_input_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="@string/default_password_hint" 
    android:textColorHint="@color/background_gray"> 
    <EditText 
     android:id="@+id/txt_password" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="textPassword"/> 
</android.support.design.widget.TextInputLayout> 

내가 다른 텍스트 크기와 View을 다시 사용하려는 때문에, 나는 사용자 지정 특성을 통해 텍스트 크기를 통과

여기 내 코드의 조각은, 설명하기 textSize :

<declare-styleable name="PasswordInputField"> 
    <attr name="textSize" format="dimension"/> 
    <attr name="hint" format="string"/> 
</declare-styleable> 

내 사용자 정의에 적용 다음과 같은 방법으로: TextInput 구성 레이아웃에서

if (attrs != null) 
{ 
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PasswordInputField, 0, 0); 
    try 
    { 
     String hint = ta.getString(R.styleable.PasswordInputField_hint); 
     if (hint != null) 
     { 
      passwordInputLayout.setHint(hint); 
     } 
      float textSize = ta.getDimensionPixelSize(R.styleable.PasswordInputField_textSize, 0); 
     if (textSize > 0) 
     { 
      passwordEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize); 
     } 
    } 
    finally 
    { 
     ta.recycle(); 
    } 
} 

답변

1

Style.xml

<style name="TextAppearence.App.TextInputLayout" parent="@android:style/TextAppearance"> 
    <item name="android:textColor">@color/white</item> 
    <item name="android:textColorHint">@color/white</item> 
    <item name="android:textSize">18sp</item> 
</style> 

   <TextInputLayout 
        android:id="@+id/til_signup_name" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:hint="@string/name" 
        app:hintTextAppearance="@style/TextAppearence.App.TextInputLayout"> 

        <EditText 
         android:id="@+id/ed_signup_name" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:textColorHint="@color/white" /> 
       </TextInputLayout> 
+0

안녕하세요, 이것은 정적 시나리오에 적합한 솔루션 인 것 같습니다. 귀하의 솔루션을 동적으로 텍스트 크기를 설정해야하는 시나리오에 연결할 수 있습니까? (사용자 지정 속성을 통해 내 사용자 지정보기로 전달되기 때문에) –

+0

이 솔루션을 사용해 보셨습니까? –

+0

죄송합니다, 프로그래밍 방식으로 텍스트 크기를 설정해야합니다 (사용자 지정보기/텍스트 크기를 사용자 지정 특성으로 전달). 따라서 정적 솔루션이 내 문제에 어떻게 적용되는지는 알 수 없습니다. –

0

당신은 문자열 recource의 크기를 설정하여 그것을 할 수 있습니다. 예를 들어

:

<string name="edittext_hint"><font size="15">Hint here!</font></string> 

은 다음 XML 단지

android:hint="@string/edittext_hint" 

이 힌트에 대한 작은 텍스트 만 입력 텍스트의 원래 크기로됩니다 물품.

또는 같은

:

MYEditText.addTextChangedListener(new TextWatcher(){ 

     @Override 
     public void afterTextChanged(Editable arg0) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void beforeTextChanged(CharSequence arg0, int arg1, 
       int arg2, int arg3) { 
      // TODO Auto-generated method stub 

     } 

     @Override 
     public void onTextChanged(CharSequence arg0, int start, int before, 
       int count) { 
      if (arg0.length() == 0) { 
       // No entered text so will show hint 
       editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mHintTextSize); 
      } else { 
       editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize); 
      } 
     } 
}); 
+0

안녕하세요, 이들은 두 가지 똑똑한 접근 방식입니다. 불행히도 두 번째 방법은 예상대로 작동하지 않았습니다. 텍스트 크기가 올바르게 트리거되고 편집 텍스트의 높이가 변경되는 경우에도 텍스트 크기는 힌트 텍스트에 영향을주지 않고 입력 텍스트에만 영향을줍니다. 첫 번째 접근법 역시 흥미 롭습니다. 그러나 다른 시나리오에서 다른 텍스트와 크기를 사용하려면 내 사용자 지정보기 구성 요소를 사용하여 XML의 힌트 텍스트를 직접 적용 할 수 없습니다. 이 방법을 프로그래밍 방식으로도 적용 할 수 있습니까? 지금 사용자 지정 속성을 통해 전체 레이아웃을 전달하려고합니다. –

0

간단한 사용이 프로그램은 ... 내가 아래에 언급 된대로 스타일을 사용자 정의 할 수 있습니다 나를

TextInputLayout til = new TextInputLayout(this); 
    til.setHintTextAppearance(android.R.style.TextAppearance_Large); 

을 위해 일하고

<style name="TextInputLayout" parent="@android:style/TextAppearance"> 
     <item name="android:textColor">@color/colorPrimaryDark</item> 
     <item name="android:textColorHint">@color/colorPrimaryDark</item> 
     <item name="android:textSize">23sp</item> 
    </style> 

TextInputLayout til = new TextInputLayout(this); 
     til.setHint("hai); 
     til.setHintTextAppearance(R.style.TextInputLayout); 
+0

@Peter F 업데이트 된 답변을 확인하십시오 ... 유용한 donot이 upvote을 잊어 버린 경우 .... –

관련 문제