设置TextEdit中提示文本的字体类型

11

是否可能在不改变EditText本身字体的情况下设置其提示框中字体的样式?谢谢。


可能是更改editText提示字体的重复问题。 - francisco_ssb
10个回答

12

似乎没有办法分别控制提示文本和正文的字体。


这个现在还是真的吗? - Stack Diego

3
如果还有人需要解决方案,请参考以下内容:

在这种情况下:

 editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                editText.typeface = ResourcesCompat.getFont(it, if (TextUtils.isEmpty(s.toString())) R.font.franklingothic_book else R.font.franklingothic_demi)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {}
        })

2
你检查过android:typeface了吗?
这对于EditText的提示和文本都有效。

是的,它会影响提示和文本。有没有一种简单的方法只更改提示? - Asahi

2

不知道您是只想让它们不同,还是您想要特别设置字体。

如果您只需要对提示文本进行不同的样式设置,则可以按照以下方式在每个字段上使用Java进行设置:

  1. Wrap your hint text in HTML style tags
  2. Pass the marked-up String to Html.fromHtml(String htmlString)
  3. Pass the result of fromHtml() to setHint();

    myField.setHint(Html.fromHtml("<i>" + hint + "</i>"));
    

1

为了使用特定字体,有一个很棒的库这里,非常简单易用。但是,如果要在android:inputType="textPassword"中使用edittext,则需要做更多的工作,下面是具体步骤:

  1. 从xml中删除android:inputType="textPassword"
  2. 使用应用字体
  3. 在代码中设置密码转换:

    password.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); password.setTransformationMethod(PasswordTransformationMethod.getInstance());


1
你应该创建CustomTypefaceSpan,以便在Hint和EditText中使用不同的字体。
Typeface hintTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf");
TypefaceSpan typefaceSpan = new CustomTypefaceSpan(hintTypeface);
SpannableString spannableString = new SpannableString("some hint text");
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
editText.setHint(spannableString);


public class CustomTypefaceSpan extends TypefaceSpan
{
    private final Typeface mNewType;

    public CustomTypefaceSpan(Typeface type)
    {
        super("");
        mNewType = type;
    }

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

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

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

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

1

你可以通过覆盖typefacefont来设置提示字体。

public class CustomEditText extends EditText {

private Context context;
private AttributeSet attrs;
private int defStyle;

public CustomEditText(Context context) {
    super(context);
    this.context = context;
    init();
}

public CustomEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.context = context;
    this.attrs = attrs;
    init();
}

public CustomEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.context = context;
    this.attrs = attrs;
    this.defStyle = defStyle;
    init();
}

private void init() {
    Typeface font = Typeface.createFromAsset(getContext().getAssets(), "opensansbold.ttf");
    this.setTypeface(font);
}

@Override
public void setTypeface(Typeface tf, int style) {
    tf = Typeface.createFromAsset(getContext().getAssets(), "opensansbold.ttf");
    super.setTypeface(tf, style);
}
}

0

处理这个问题的最佳方法是在EditText中添加TextWatcher,并在EditText中没有输入时动态更改字体。


0
mEmailView.setTypeface(Typeface.createFromAsset(getAssets(),
    getString(R.string.app_font)));

((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(
     getAssets(), getString(R.string.app_font)));

你能为这段代码添加一些解释吗?此外,这与Shahid的答案有何不同? - Tot Zam
1
你必须为TextInputLayout和EditText都设置Typeface,而不仅仅是EditText。 - Mohammad

-3

editText.setTypeface(Typeface.SERIF);


editText.setTypeface(Typeface.SERIF); 可以用来改变EditText的字体类型。 - Ahmed Nader

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