4

입력란이 EditText입니다. 나는 그것에 힌트를 추가했다. 이제 힌트 텍스트의 크기를 변경하고 싶지만, 이렇게하면 텍스트의 크기에도 영향을줍니다. 힌트와 텍스트의 크기를 개별적으로 변경하는 방법을 안내하고 힌트와 텍스트에 다른 글꼴을 제공합니다.EditText에서 텍스트 크기를 변경하지 않고 힌트 텍스트 크기를 변경하는 방법

<EditText 
    android:layout_width="0dp" 
    android:layout_height="50dp" 
    android:layout_weight="1" 
    android:textSize="12sp" 
    android:textColor="#ffffff"        
    android:fontFamily="sans-serif-light" 
    android:hint="MM/YY" 
    android:textColorHint="@color/white" /> 

답변

5

힌트와 텍스트는 배타적이며, 그 중 하나가 표시되면 다른 하나는 표시되지 않습니다.

이 때문에 비어 있거나 (힌트가 보이는지) 텍스트가 보이는지에 따라 EditText의 속성을 변경할 수 있습니다. 예를 들어

:

final EditText editText = (EditText) findViewById(R.id.yourEditText); 

editText.addTextChangedListener(new TextWatcher() { 
    boolean hint; 

    @Override 
    public void beforeTextChanged(CharSequence s, int start, int count, int after) { 
    } 

    @Override 
    public void onTextChanged(CharSequence s, int start, int before, int count) { 
     if(s.length() == 0) { 
      // no text, hint is visible 
      hint = true; 
      editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12); 
      editText.setTypeface(Typeface.createFromAsset(getAssets(), 
       "hintFont.ttf")); // setting the font 
     } else if(hint) { 
      // no hint, text is visible 
      hint = false; 
      editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); 
      editText.setTypeface(Typeface.createFromAsset(getAssets(), 
       "textFont.ttf")); // setting the font 
     } 
    } 

    @Override 
    public void afterTextChanged(Editable s) { 
    } 
}); 
16

리소스 파일에서 설정할 수 있습니다. 예를 들어

: 다음 몇 가지 후 텍스트 크기를 변경하려면 텍스트 리스너를 설정

android:hint="@string/hint" 
+1

내가뿐만 아니라'Font'가 추가 할 수 있습니까? – Kirmani88

+0

예, 예를 들어 텍스트의 색상을 지정할 수도 있습니다. –

+0

이 문제를 해결하는 가장 쉬운 방법이라고 생각합니다. 자바 코드로 처리하려면 많은 줄의 코드가 필요합니다. –

1

당신은 작은, 원하는 값으로 텍스트 크기를 설정할 수 있습니다 :

<string name="hint"><font size="20">Hint!</font></string> 

그리고 당신의 XML

텍스트가 입력됩니다.

0

사용하여 HTML 괜찮습니다,하지만 유연하지 않습니다. 예를 들어, 정확한 크기를 설정할 수 없습니다. 난 당신이 설정할 수있는 대체 솔루션을 제공 할 것입니다 :

  • 새로운 힌트 글꼴
  • 새로운 힌트 크기
  • 새로운 힌트 스타일

1) 사용자 정의 Hint 객체 생성 :

import android.graphics.Typeface; 
import android.text.SpannableString; 
import android.text.Spanned; 
import android.text.style.MetricAffectingSpan; 

public class CustomHint extends SpannableString 
{ 
    public CustomHint(final CharSequence source, final int style) 
    { 
     this(null, source, style, null); 
    } 

    public CustomHint(final CharSequence source, final Float size) 
    { 
     this(null, source, size); 
    } 

    public CustomHint(final CharSequence source, final int style, final Float size) 
    { 
     this(null, source, style, size); 
    } 

    public CustomHint(final Typeface typeface, final CharSequence source, final int style) 
    { 
     this(typeface, source, style, null); 
    } 

    public CustomHint(final Typeface typeface, final CharSequence source, final Float size) 
    { 
     this(typeface, source, null, size); 
    } 

    public CustomHint(final Typeface typeface, final CharSequence source, final Integer style, final Float size) 
    { 
     super(source); 

     MetricAffectingSpan typefaceSpan = new CustomMetricAffectingSpan(typeface, style, size); 
     setSpan(typefaceSpan, 0, source.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE); 
    } 
} 

2) c ustom MetricAffectingSpan 목적 :

import android.graphics.Typeface; 
import android.text.TextPaint; 
import android.text.style.MetricAffectingSpan; 

public class CustomMetricAffectingSpan extends MetricAffectingSpan 
{ 
    private final Typeface _typeface; 
    private final Float _newSize; 
    private final Integer _newStyle; 

    public CustomMetricAffectingSpan(Float size) 
    { 
     this(null, null, size); 
    } 

    public CustomMetricAffectingSpan(Float size, Integer style) 
    { 
     this(null, style, size); 
    } 

    public CustomMetricAffectingSpan(Typeface type, Integer style, Float size) 
    { 
     this._typeface = type; 
     this._newStyle = style; 
     this._newSize = size; 
    } 

    @Override 
    public void updateDrawState(TextPaint ds) 
    { 
     applyNewSize(ds); 
    } 

    @Override 
    public void updateMeasureState(TextPaint paint) 
    { 
     applyNewSize(paint); 
    } 

    private void applyNewSize(TextPaint paint) 
    { 
     if (this._newStyle != null) 
      paint.setTypeface(Typeface.create(this._typeface, this._newStyle)); 
     else 
      paint.setTypeface(this._typeface); 

     if (this._newSize != null) 
      paint.setTextSize(this._newSize); 
    } 
} 

3) 사용

Typeface newTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf"); 
CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC, 60f); 
     //  CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC); 
     //  CustomHint customHint = new CustomHint(newTypeface, "Enter some text", 60f); 
     //  CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC, 60f); 
     //  CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC); 
     //  CustomHint customHint = new CustomHint("Enter some text", 60f); 

customEditText.setHint(customHint); 
관련 문제