2011-10-13 133 views
6

我想爲不同的字體樣式提示和字體樣式在edittext文本中輸入。 例如。可以說,提示字體大小是12和它的正常類型。但是當用戶開始在edittext中輸入時,輸入文本的字體大小應該變爲14並且粗體。再次如果用戶刪除文本提示應該是上述類型。不同的提示字體樣式和鍵入的文本字體樣式android

回答

4

您可以以編程方式更改提示顏色,使其通過使用下面的代碼

editTextId.setHintTextColor(Color.alpha(006666)); 
0

已經給出的答案是正確的,但目前指定不同的顏色可能也是在在EditText上輸入字體樣式不同XML文件通過android:textColorHint屬性。舉例來說,你可以做這樣的事情(假設你已經正確定義my_favourite_colour作爲一種資源):

<EditText 
... other properties here ... 
android:textColorHint="@color/my_favourite_colour" 
</EditText> 
0

可以使用SpannableStringMetricAffectingSpan實現它。您將可以更改提示的字體,大小和樣式。

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)創建的自定義對象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);