2011-10-13 33 views
6

Tôi muốn có kiểu phông chữ khác nhau cho gợi ý và kiểu phông chữ cho văn bản đã nhập trong văn bản. Ví dụ: Cho phép nói kích thước phông chữ gợi ý là 12 và loại bình thường của nó. Nhưng khi người dùng bắt đầu nhập vào văn bản, kích thước phông chữ của văn bản đã nhập sẽ trở thành 14 và in đậm. một lần nữa nếu người dùng xóa văn bản gợi ý phải thuộc loại được đề cập ở trên.Kiểu phông chữ gợi ý khác nhau và được nhập theo kiểu phông chữ văn bản android

Trả lời

4

Bạn lập trình có thể thay đổi màu sắc gợi ý để làm cho nó khác biệt so với kiểu chữ gõ vào EditText bằng cách sử dụng đoạn mã sau

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

Câu trả lời đã được đưa ra là đúng nhưng hiện tại chỉ định một màu sắc khác nhau có thể còn trong Tệp XML thông qua thuộc tính android: textColorHint thuộc tính . Ví dụ bạn có thể làm một cái gì đó như thế này (giả sử bạn đã xác định my_favourite_colour bạn một cách chính xác như một nguồn lực):

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

Bạn có thể đạt được nó bằng cách sử SpannableStringMetricAffectingSpan. Bạn sẽ có thể thay đổi phông chữ, kích thước và kiểu của gợi ý.

1) Tạo một đối tượng tùy chỉnh 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) Tạo tùy chỉnh MetricAffectingSpan đối tượng:

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) Sử dụng:

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); 
Các vấn đề liên quan