更改EditText提示的字体



是否可以更改EditText字段中显示的提示的字体?我想在XML本身中设置字体。

您可以使用SpannableString和自定义类型FaceSpan更改它。

首先,创建一个自定义类型Facespan类:

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

然后将TypeFacespan设置为spanbablestring:

TypefaceSpan typefaceSpan = new CustomTypefaceSpan(typeface);
SpannableString spannableString = new SpannableString(hintText);
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

最终只是设置了您的EditText的提示:

mEditText.setHint(spannableString);

我没有找到任何有用的更改提示字体的有用方法。但是您可以这样实现:

mEt.addTextChangedListener(new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }
    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(s.length()== 0) {
            //mEt.setTypeFace(normalFont);
        }else{
           // mEt.setTypeFace(hintFont);
        }
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
});

有一种非常简单的方法可以做到。我只是在我的应用程序中做到了。钥匙也与EditText一起设置了textInputlayout的faceType。

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

@francisco_ssb的答案是正确的。但是,我将提供一种替代解决方案,该解决方案不仅有助于更改提示字体,还可以更改其大小和样式。我希望该解决方案会有所帮助。

1)创建一个自定义Hint对象:

import android.graphics.Typeface;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.MetricAffectingSpan;
public class CustomHint extends SpannableString
{
    public CustomHint(final CharSequence source, final int style)
    {
        this(null, source, style, null);
    }
    public CustomHint(final CharSequence source, final Float size)
    {
        this(null, source, size);
    }
    public CustomHint(final CharSequence source, final int style, final Float size)
    {
        this(null, source, style, size);
    }
    public CustomHint(final Typeface typeface, final CharSequence source, final int style)
    {
        this(typeface, source, style, null);
    }
    public CustomHint(final Typeface typeface, final CharSequence source, final Float size)
    {
        this(typeface, source, null, size);
    }
    public CustomHint(final Typeface typeface, final CharSequence source, final Integer style, final Float size)
    {
        super(source);
        MetricAffectingSpan typefaceSpan = new CustomMetricAffectingSpan(typeface, style, size);
        setSpan(typefaceSpan, 0, source.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    }
}

2)创建自定义MetricAffectingSpan对象:

import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.MetricAffectingSpan;
public class CustomMetricAffectingSpan extends MetricAffectingSpan
{
    private final Typeface _typeface;
    private final Float    _newSize;
    private final Integer  _newStyle;
    public CustomMetricAffectingSpan(Float size)
    {
        this(null, null, size);
    }
    public CustomMetricAffectingSpan(Float size, Integer style)
    {
        this(null, style, size);
    }
    public CustomMetricAffectingSpan(Typeface type, Integer style, Float size)
    {
        this._typeface = type;
        this._newStyle = style;
        this._newSize = size;
    }
    @Override
    public void updateDrawState(TextPaint ds)
    {
        applyNewSize(ds);
    }
    @Override
    public void updateMeasureState(TextPaint paint)
    {
        applyNewSize(paint);
    }
    private void applyNewSize(TextPaint paint)
    {
        if (this._newStyle != null)
            paint.setTypeface(Typeface.create(this._typeface, this._newStyle));
        else
            paint.setTypeface(this._typeface);
        if (this._newSize != null)
            paint.setTextSize(this._newSize);
    }
}

3)使用:

Typeface newTypeface = Typeface.createFromAsset(getAssets(), "AguafinaScript-Regular.ttf");
CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC, 60f);
        //        CustomHint customHint = new CustomHint(newTypeface, "Enter some text", Typeface.BOLD_ITALIC);
        //        CustomHint customHint = new CustomHint(newTypeface, "Enter some text", 60f);
        //        CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC, 60f);
        //        CustomHint customHint = new CustomHint("Enter some text", Typeface.BOLD_ITALIC);
        //        CustomHint customHint = new CustomHint("Enter some text", 60f);
customEditText.setHint(customHint);

在xml-

中不可能

文本和提示只能在xml中具有相同的字体。

我可以通过使用此库来更改提示字体。

编译库后,您必须创建一类应用程序和类定义以下命令:

CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
                  .setDefaultFontPath("font.ttf")
                  .setFontAttrId(R.attr.fontPath)
                  .build()
          );

在每次活动之后,您要使用以下命令将其覆盖:

@Override
      protected void attachBaseContext(Context newBase) {
          super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
      }

采用使用文本输入布局并放置文本输入文本

的方法
 <com.google.android.material.textfield.TextInputLayout
    android:id="@+id/text_input_layout_Til">
   <com.google.android.material.textfield.TextInputEditText
      android:id="@+id/editText"/>
 </com.google.android.material.textfield.TextInputLayout>

在片段/活动上,将提示的字体设置为textInputlayout( text_input_layout_til )然后在编辑文本上设置字体( dedittext )/如果执行此操作,用户将输入的文本,提示将具有不同的字体

使用Android 8.0(API级别26)出现了一个新功能,它有可能在EditText中更改XML中的字体。基于XML中字体的指南。

步骤:

1)创建一个新的资源目录:右键单击 res 文件夹,然后转到 new -> new-> Android Resource Directory 。p> 2)将资源目录的名称设置为 font

3)资源类型列表中,选择 font ,然后单击"确定"。

4) font 文件夹中添加您的自定义字体(例如my_font.ttf)。

5)在布局XML中,将 fontfamily 属性设置为字体文件:

<EditText
    android:id="@+id/edit_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/my_font"/>

...或如果您想以编程方式进行操作:

val typeface = resources.getFont(R.font.my_font)
edit_text.typeface = typeface

在运行Android 4.1(API级16)及更高的设备上使用XML功能中的字体,请使用支持库26:

注意:当您通过支持库中的XML布局中的字体系列在XML布局中,使用应用程序空间来确保您的字体加载。

1) in font 目录创建 custom_font.xml

<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto">
    <font app:fontStyle="normal" app:fontWeight="400" app:font="@font/my_font"/>
</font-family>

2)设置 fontfamily 属性:

 <EditText
    android:id="@+id/edit_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/custom_font"/>

...或如果您想以编程方式进行操作:

val typeface = ResourcesCompat.getFont(context, R.font.custom_font)
edit_text.typeface = typeface

在kotlin中:

            nomeEditText.setText("New Text") //changes the text
            nomeEditText.isEnabled = false //disables editable
            nomeEditText.setBackgroundColor(Color.parseColor("#ffffff")) //changes background
            nomeEditText.setTextColor(Color.parseColor("#737373")) //changes color text
            nomeEditText.setTextSize(TypedValue.COMPLEX_UNIT_SP,28f) //changes text size
            nomeEditText.typeface = Typeface.create("@font/roboto", Typeface.BOLD) //changes font familly
            nomeEditText.gravity = Gravity.CENTER_HORIZONTAL //centralize

使用此代码:

edittext.setAccentTypeface(typeface);

最新更新