簡體   English   中英

Android-自定義EditText前綴顏色

[英]Android - Custom EditText prefix color

我已經借助SO和Copy Paste為我的自定義EditText創建了前綴修改。

這是prefix的特定代碼:

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="$"/>

此代碼產生:

這個圖片

但是問題是,如果我設置textColorHint ,則前綴的顏色將與提示的顏色相同,例如:

此橙色文字

如何將前綴的顏色修改為指定的顏色? 就我而言,是讓它具有自己的顏色,而不是使用提示顏色。

更新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);
    }
}

試試這些:

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)`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM