更改EditText的字体系列setError()

时间:2014-01-21 11:17:01

标签: android css fonts android-edittext

我想更改EditText setError()的字体系列。

我试过这个:

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

当用户选择其他字体时,我想更改Samsung手机中错误消息的字体系列。

任何想法都会受到赞赏。

提前致谢

2 个答案:

答案 0 :(得分:4)

你可以试试这个:

Application

然后你还在项目中包含了这个文件:

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

答案 1 :(得分:0)

1)添加这个类:

.png

}

2)在您的活动中使用此代码:

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

3)在您的活动中添加此方法:

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

4)最后像这样使用它:

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