Android 更改editText提示的字体
是否可以更改Android 更改editText提示的字体,android,android-edittext,Android,Android Edittext,是否可以更改EditText字段中显示的提示的字体?我想在xml本身中设置字体。这在xml中是不可能的- 文本和提示在XML中只能具有相同的字体。我还没有找到任何有用的方法来更改XML中的提示字体。但您可以实现如下目的: mEt.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int af
EditText
字段中显示的提示的字体?我想在xml本身中设置字体。这在xml中是不可能的-
文本和提示在XML中只能具有相同的字体。我还没有找到任何有用的方法来更改XML中的提示字体。但您可以实现如下目的:
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) {
}
});
您可以使用一个SpannableString和一个自定义字体span来更改它 首先,创建一个自定义TypefaceSpan类:
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);
}
}
然后只需将字体span设置为SpannableString:
TypefaceSpan typefaceSpan = new CustomTypefaceSpan(typeface);
SpannableString spannableString = new SpannableString(hintText);
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
最后,只需设置EditText的提示:
mEditText.setHint(spannableString);
有一个非常简单的方法。我只是在我的应用程序中这样做了,它工作了。 该键与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)));
我可以用这个来改变提示字体
编译库后,必须创建一个应用程序类,并使用以下命令创建类定义:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
在每个要覆盖的活动之后,请使用以下命令:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@francisco_ssb的答案是正确的。但是,我将提供一个替代解决方案,它不仅可以帮助更改提示的字体,还可以更改提示的大小和样式。我希望这个解决方案会有所帮助 1)创建自定义
提示
对象:
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);
}
}
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);
}
}
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);
2)创建自定义MetricAffectingSpan
对象:
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);
}
}
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);
}
}
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);
3)使用:
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);
}
}
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);
}
}
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);
使用此代码:
edittext.setAccentTypeface(typeface);
Android8.0(API级别26)提供了一个新功能,可以根据EditText中显示的提示更改xml字体。指南基于
步骤:
1)创建新的资源目录:右键单击res文件夹并转到new->Android资源目录
2)将资源目录的名称设置为font
3)在资源类型列表中,选择字体,然后单击确定
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"/>
要在运行Android 4.1(API级别16)及更高版本的设备上使用XML字体功能,请使用支持库26:
注意:当您通过支持库在XML布局中声明字体系列时,请使用应用程序命名空间以确保字体加载
1)在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>
采用使用文本输入布局并在其中放置文本输入编辑文本的方法
<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\u input\u layout\u Til)
然后在编辑文本(editText)上也设置字体/
如果执行此操作,用户将输入的文本和提示将在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
可能重复:正如所指出的,这是一个重复。您可以更改提示颜色,但不能更改字体。您可以检查我的答案,希望它有效。这对我来说是正确的答案,谢谢@francisco_ssb。它工作得很好。跨度的使用太棒了!在我的例子中,我不需要真正的“自定义字体”,只需要一个与常规文本不同的系统字体和外观。我在提示中使用了TextAppearanceSpan,它就像一个符咒一样工作。但是,这种方法不会浮动当前提示。有没有建议如何使其浮动到上述字段@LisaWray@mochadwi您所想到的“浮动”可能是TextInputLayout的一种效果,TextInputLayout是EditText的一种材料设计包装。我现在在手机上,但只有谷歌:)我明白了,所以,有了这段代码,再加上TextInputLayout上的提示,应该可以做到吗*cmiiw@lisawray编译库后,必须创建一个应用程序类和以下命令的类定义:CalligraphyConfig.initDefault(new CalligraphyConfig.Builder().setDefaultFontPath(“font.ttf”).setFontAttrId(R.attr.fontPath.build());在您希望使用以下命令覆盖它的每个活动之后:@Override protected void attachBaseContext(Context-newBase){super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));}@johnny5,这个答案在我使用这个答案所指的书法库时对我有帮助。HOSHYAR Ahmadpour也解释了答案。谢谢你的回答HOSHYAR Ahmadpour。@Sakiboy是的,那很好,我在审查队列中保证未来的人能够使用这个答案,在他修正了答案简单的解决方案后,我给了他更高的票数,这是应该的be@Dariuslol实际上看起来没有更好的了…这不适用于EditText。