私は自分のカスタム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
を設定した場合
しかし、問題がある、接頭辞の色のようなヒントの色と同じになります。指定した色にプレフィックスの色を変更する方法
?私の場合、ヒントカラーを使用するのではなく、独自の色を持たせることです。
おそらくたいが 'フィールドオブジェクトPaint'ことを確認し、他の場所でそれを初期化したいです、特に 'EditText'では。 –
これはトリックでした!しかし、どのようにtho?編集:Nevermind、私は盲目的に 'getPaint() 'を使用して実現した。しかし、なぜ 'getPaint()'はヒントカラーを返しますか? –
ペイント – Pehlaj