在android中更改文本编辑错误通知字体

Ham*_*FzM 5 android typeface android-edittext

我知道我们可以使用改变编辑文本字体Typeface.但是我们为编辑文本设置的错误呢?看下面的代码:

Typeface font = Typeface.createFromAsset(getAssets(), "fonts/ATaha.ttf");
private EditText mPasswordView;
mPasswordView = (EditText) findViewById(R.id.password);
mPasswordView.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)

使用此代码我只能更改编辑文本字体但是当我设置这样的错误时:

mPasswordView.setError(getString(R.string.error_field_required));
Run Code Online (Sandbox Code Playgroud)

错误通知字体是android默认字体,并且不使用类型face更改.我怎么能改变呢?

mya*_*mal 6

您可以使用a SpannableString来设置字体:

SpannableString s = new SpannableString(errorString);
s.setSpan(new TypefaceSpan(font), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mPasswordView.setError(s);
Run Code Online (Sandbox Code Playgroud)

Span具有特定Typeface集的自定义类:

public class TypefaceSpan extends MetricAffectingSpan {
    private Typeface mTypeface;
    public TypefaceSpan(Typeface typeface) {
        mTypeface = typeface;
    }

    @Override
    public void updateMeasureState(TextPaint p) {
        p.setTypeface(mTypeface);
        p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setTypeface(mTypeface);
        tp.setFlags(tp.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
    }
}
Run Code Online (Sandbox Code Playgroud)