2017-03-29 9 views
1

私は自分のカスタムEditTextのプレフィックス修正をSOとコピー貼り付けによって作成しました。ここで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="$"/> 

このコードが生成する:私はtextColorHintを設定した場合

This Image

しかし、問題がある、接頭辞の色のようなヒントの色と同じになります。指定した色にプレフィックスの色を変更する方法

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

これはトリックでした!しかし、どのようにtho?編集:Nevermind、私は盲目的に 'getPaint() 'を使用して実現した。しかし、なぜ 'getPaint()'はヒントカラーを返しますか? –

+0

ペイント – 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

AhhカスタムTypefaceSpan ..私もそれを使用してきましたが、それは接頭辞ではありませんでした。私は笑を意味しました。しかし、私の投稿されたコードと、 –

関連する問題