更改EditText.setError()的字体族。

3

我希望能够更改EditText setError()的字体。

我尝试过以下方法:

editText.setError(Html.fromHtml("<span style=\"font-family: sans-serif !important\">hello</span>"));

我希望您能够帮我翻译一下,当用户选择其他字体时,如何更改三星手机中错误消息的字体。如果您有任何想法,请告诉我。谢谢。

在important关键字后面加上分号有帮助吗?这是有效的CSS... - Manuel Allenspach
谢谢你的回答,Manu。我已经尝试过了,但还是不行。 - sastresa
本文解释了如何为编辑文本添加样式:https://dev59.com/9WYq5IYBdhLWcg3whQ_V - Manuel Allenspach
谢谢,Manu。我会尝试的。 - sastresa
2个回答

5

你可以尝试这个:

Typeface typeFaceDefault = Typeface.createFromAsset(getAssets(), "BYekan.ttf");

TextInputLayout inputLayout = (TextInputLayout) view.findViewById(R.id.lyt_input); 

SpannableStringBuilder ssbuilder = new SpannableStringBuilder(getString(R.string.err_wrong_input));
ssbuilder.setSpan(new CustomTypefaceSpan("", ActivityMain.typeFaceDefault), 0, ssbuilder.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);    
inputLayout.setError(ssbuilder);

如果您已经将这个文件包含在您的项目中:

public class CustomTypefaceSpan extends TypefaceSpan {

    private final Typeface newType;

    public CustomTypefaceSpan(String family, Typeface type) {
        super(family);
        newType = type;
    }

    @Override
    public void updateDrawState(TextPaint ds) {
        applyCustomTypeFace(ds, newType);
    }

    @Override
    public void updateMeasureState(TextPaint paint) {
        applyCustomTypeFace(paint, newType);
    }

    private static void applyCustomTypeFace(Paint paint, Typeface tf) {
        int oldStyle;
        Typeface old = paint.getTypeface();
        if (old == null) {
            oldStyle = 0;
        } else {
            oldStyle = old.getStyle();
        }

        int fake = oldStyle & ~tf.getStyle();
        if ((fake & Typeface.BOLD) != 0) {
            paint.setFakeBoldText(true);
        }

        if ((fake & Typeface.ITALIC) != 0) {
            paint.setTextSkewX(-0.25f);
        }

        paint.setTypeface(tf);
    }
}

0

1) 添加这个类:

import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;

public class CustomTypefaceSpan extends TypefaceSpan {
public static final Parcelable.Creator<CustomTypefaceSpan> CREATOR = new Parcelable.Creator<CustomTypefaceSpan>() {

    public CustomTypefaceSpan createFromParcel(Parcel in) {
        return new CustomTypefaceSpan(in);
    }

    public CustomTypefaceSpan[] newArray(int size) {
        return new CustomTypefaceSpan[size];
    }
};
private final Typeface newType;

public CustomTypefaceSpan(String family, Typeface type) {
    super(family);
    newType = type;
}

private CustomTypefaceSpan(Parcel in) {
    super(in.readString());
    newType = Typeface.createFromFile(in.readString());
}

private static void applyCustomTypeFace(Paint paint, Typeface tf) {
    int oldStyle;
    Typeface old = paint.getTypeface();
    if (old == null) {
        oldStyle = 0;
    } else {
        oldStyle = old.getStyle();
    }

    int fake = oldStyle & ~tf.getStyle();
    if ((fake & Typeface.BOLD) != 0) {
        paint.setFakeBoldText(true);
    }

    if ((fake & Typeface.ITALIC) != 0) {
        paint.setTextSkewX(-0.25f);
    }

    paint.setTypeface(tf);
}

@Override
public void updateDrawState(TextPaint ds) {
    applyCustomTypeFace(ds, newType);
}

@Override
public void updateMeasureState(TextPaint paint) {
    applyCustomTypeFace(paint, newType);
}

}

2) 在你的活动中使用这段代码:

Typeface typeFaceDefault = Typeface.createFromAsset(getAssets(), "yourFont.ttf");

3)在你的活动中添加这个方法:

   public void setErrText(CharSequence err,EditText view){
    SpannableStringBuilder ssbuilder = new SpannableStringBuilder(err);
    ssbuilder.setSpan(new CustomTypefaceSpan("", typeFaceDefault), 0, ssbuilder.length(), Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
    view.setError(ssbuilder);
}

4) 最后使用它的方式如下:

setErrText(getString(R.string.your_err_txt),yourEdt);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接