2017-03-29 74 views
1

我已通過SO和複製粘貼的幫助爲我的自定義EditText創建了前綴修改。Android - 自定義EditText前綴顏色

下面是前綴的特定代碼:

private String mPrefix = ""; 
private Rect mPrefixRect = new Rect(); 

public OpenSansEditText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    applyCustomFont(context, attrs); 
    applyPrefix(context, attrs); 
} 

public OpenSansEditText(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    applyCustomFont(context, attrs); 
    applyPrefix(context, attrs); 
} 


private void applyPrefix(Context context, AttributeSet attrs) { 
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.OpenSansET); 
    String fontFace; 

    try { 
     fontFace = a.getString(R.styleable.OpenSansET_prefix); 
    } finally { 
     a.recycle(); 
    } 
    if (fontFace != null){ 
     mPrefix = fontFace; 
    } else { 
     mPrefix = ""; 
    } 
} 

protected void setmPrefix(String prefix){ 
    this.mPrefix = prefix; 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    if (!mPrefix.equals("")){ 
     getPaint().getTextBounds(mPrefix, 0, mPrefix.length(), mPrefixRect); 
     mPrefixRect.right += getPaint().measureText(" "); // add some offset 
    } 

    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if (!mPrefix.equals("")) { 
     canvas.drawText(mPrefix, super.getCompoundPaddingLeft(), getBaseline(), getPaint()); 
    } 
} 

@Override 
public int getCompoundPaddingLeft() { 
    return mPrefix.equals("") ? super.getCompoundPaddingLeft() 
      : super.getCompoundPaddingLeft() + mPrefixRect.width(); 
} 

基本上它是,如果我從XML或通過代碼提供的前綴,它吸引前綴到EditText的開始。例如:

<com.asta.classes.OpenSansEditText 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="@string/min" 
    app:prefix="$"/> 

此代碼生成:

This Image

但問題是,如果我設置textColorHint,前綴的顏色將是一樣的提示一樣的顏色:

This Orange Text

如何修改前綴的顏色指定的顏色?在我的情況下,它是讓它有自己的顏色,而不是使用提示顏色。

回答

1

更新的onDraw方法

如:

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    if (!mPrefix.equals("")) { 
     Paint paint = getPaint(); 
     paint.setColor(color); 
     canvas.drawText(mPrefix, super.getCompoundPaddingLeft(), getBaseline(), paint); 
    } 
} 
+1

你可能想作出這樣的'Paint'對象中的字段,以及其他地方對其進行初始化,特別是在「EditText」中。 –

+1

這個伎倆!但是,怎麼樣?編輯:沒關係,所以我只是意識到我盲目地使用getPaint()。但爲什麼'getPaint()'返回提示顏色? –

+0

您可以使用Paint – Pehlaj

1

嘗試這些:

Typeface font = Typeface.createFromAsset(getAssets(), "OpenSans_Semibold.ttf"); 


SpannableStringBuilder builder = new SpannableStringBuilder(); 

String a = "$" + " "; 

SpannableString aSpannable = new SpannableString(a); 

aSpannable.setSpan(newForegroundColorSpan(getResources().getColor(R.color.color_yellow)), 0, a.length(), 0); 

aSpannable.setSpan(new CustomTypefaceSpan("", font), 0, a.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE); 

builder.append(aSpannable); 



String b = "Min"; 

SpannableString bSpannable = new SpannableString(b); 

bSpannable.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.color_white)), 0, b.length(), 0); 


bSpannable.setSpan(new CustomTypefaceSpan("", font), 0, b.length(),  Spanned.SPAN_EXCLUSIVE_INCLUSIVE); 

builder.append(bSpannable); 


final EditText textV = new EditText(this); 

textV.setHint(builder,textV.BufferType.SPANNABLE)` 
+0

啊自定義TypefaceSpan ..我也一直在使用,但這不是我的前綴哈哈,謝謝,雖然,看到我發佈的代碼和接受的答案如何 –