2011-10-13 18 views
6

Desidero avere uno stile di carattere diverso per il suggerimento e lo stile del carattere per il testo digitato in edittext. Ad es. Diciamo che la dimensione del font suggerimento è 12 e il suo tipo normale. Ma quando l'utente inizia a digitare in edittext, la dimensione del carattere del testo digitato dovrebbe diventare 14 e in grassetto. di nuovo, se l'utente rimuove il testo, il suggerimento deve essere del tipo sopra indicato.Stile del font di suggerimento diverso e digitato nello stile di carattere del testo android

risposta

4

È possibile modificare a livello di codice il colore suggerimento per renderlo diverso da stile del carattere digitato in EditText utilizzando il seguente codice

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

La risposta già data è corretta ma attualmente specificare un colore diverso è possibile anche nel File XML tramite l'attributo : textColorHint. Per esempio si potrebbe fare qualcosa di simile (supponendo che hai definito il tuo my_favourite_colour correttamente come una risorsa):

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

si può raggiungere utilizzando SpannableString e MetricAffectingSpan. Sarai in grado di modificare il font, la dimensione e lo stile del suggerimento.

1) Creazione di un oggetto personalizzato 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) Creare personalizzato MetricAffectingSpan oggetto:

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) Usa:

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); 
Problemi correlati