在TextEdit中设置提示的字体

时间:2011-01-17 19:23:16

标签: android user-interface view

是否可以设置EditText视图的提示元素的字体而不更改EditText本身的字体?感谢。

11 个答案:

答案 0 :(得分:10)

似乎无法单独控制文本提示和字体的字体。

答案 1 :(得分:2)

您是否检查了android:typeface

这适用于EditText的提示和文本。

答案 2 :(得分:2)

不知道你是否需要让它们变得不同,或者你是否想要专门设置字体。

如果您只需要以与文本不同的方式设置提示样式,那么您可以在每个字段的基础上使用Java进行设置

  1. 将提示文本换行为HTML样式标记
  2. 将标记的字符串传递给Html.fromHtml(String htmlString)
  3. 将fromHtml()的结果传递给setHint();

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

答案 3 :(得分: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);
}
}

答案 4 :(得分:1)

为了拥有特定的字体,有一个很棒的库here,它非常简单,但要有一个带有android的编辑文本:inputType =&#34; textPassword&#34;你应该多做一点,所以这就是:

  1. 删除android:inputType =&#34; textPassword&#34;来自xml
  2. 使用此library
  3. 应用字体
  4. 在代码中设置passwordtransformation:

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

答案 5 :(得分:1)

万一有人仍然需要解决方案:

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

答案 6 :(得分:0)

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

答案 7 :(得分:0)

您无需做任何事情。只需将字体设置为EditText,即可自动更改提示字体。

这是自定义字体以及如何设置它:

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/gobold_uplow.ttf");

Edittext mEmailEntry = (EditText) findViewById(R.id.entry_email);
mEmailEntry.setTypeface(typeface);

答案 8 :(得分: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)));

答案 9 :(得分:0)

您应该创建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);
    }
}

答案 10 :(得分:-3)

editText.setTypeface(Typeface.SERIF);