【问题标题】:Change font family of EditText setError()更改 EditText setError() 的字体系列
【发布时间】:2018-04-02 05:43:05
【问题描述】:

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

我试过了:

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

当用户选择另一种字体时,我想更改三星手机中错误消息的字体系列。

任何想法都将不胜感激。

提前致谢

【问题讨论】:

  • 重要后加分号有用吗?那将是有效的 css...
  • 感谢Manu的回答,我试过了,还是不行
  • 这篇文章解释了如何设置编辑文本的样式:stackoverflow.com/questions/14413575/…
  • 谢谢Manu,我会试试的。

标签: android css fonts android-edittext


【解决方案1】:

你可以试试这个:

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-01
      • 2019-04-26
      • 2020-06-26
      • 2011-05-26
      • 1970-01-01
      • 2015-02-05
      • 2013-01-09
      • 2020-02-01
      相关资源
      最近更新 更多