2016-09-06 23 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> 

と私のカスタムでそれを適用View次のように: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

こんにちは、これは2つの巧妙なアプローチです。残念ながら、2番目の方法は期待どおりには機能しませんでした。テキストサイズの変更が正しくトリガーされ、編集テキストの高さが変更されても、テキストサイズはヒントテキストには影響せず、入力テキストにのみ影響します。 最初のアプローチも面白いです。ただし、カスタムビューコンポーネントでは、さまざまなシナリオで異なるテキストとサイズを使用する場合、ヒントテキストを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を忘れました.... –

関連する問題