能否更改EditText
字段中显示的提示字体? 我希望在xml文件中设置字体。
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设置为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);
我还没有找到任何有用的方法来在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) {
}
});
有一种非常简单的方法来解决这个问题。我在我的应用程序中刚刚尝试了一下,它起作用了。 关键是设置TextInputLayout的Facetype,同时也要设置EditText的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)));
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);
我可以使用这个库来更改提示字体。
编译库后,您必须创建一个应用程序类,并按照以下命令定义类:
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
在XML中不可能做到-
在XML中,文本和提示只能使用相同的字体。
<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>
随着Android 8.0(API级别26)的推出,新增了一个功能,可以在EditText中更改XML中显示的提示字体。根据XML中的字体指南。
步骤:
1) 创建一个新的资源目录:右键单击res文件夹,然后转到新建-> Android资源目录。
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中的字体功能,请使用Support Library 26:
注意:当您通过支持库在XML布局中声明字体系列时,请使用app命名空间以确保您的字体加载。
1) 在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
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);
Typeface typeface = Typeface.createFromAsset(getAssets(), "font.ttf"); EditText editText = findViewById(R.id.editText); editText.setTypeface(typeface); // 设置EditText中文本的字体样式 editText.setHint("请输入内容"); // 设置EditText中提示文本的内容 editText.setHintTextColor(Color.GRAY); // 设置EditText中提示文本的颜色
请注意,您需要将“font.ttf”替换为您自己的字体文件名,并将其放入“assets”文件夹中。 - thepoosh