是否可能在不改变EditText本身字体的情况下设置其提示框中字体的样式?谢谢。
是否可能在不改变EditText本身字体的情况下设置其提示框中字体的样式?谢谢。
似乎没有办法分别控制提示文本和正文的字体。
在这种情况下:
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) {}
})
不知道您是只想让它们不同,还是您想要特别设置字体。
如果您只需要对提示文本进行不同的样式设置,则可以按照以下方式在每个字段上使用Java进行设置:
Pass the result of fromHtml() to setHint();
myField.setHint(Html.fromHtml("<i>" + hint + "</i>"));
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);
}
}
你可以通过覆盖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);
}
}
处理这个问题的最佳方法是在EditText中添加TextWatcher,并在EditText中没有输入时动态更改字体。
mEmailView.setTypeface(Typeface.createFromAsset(getAssets(),
getString(R.string.app_font)));
((TextInputLayout) findViewById(R.id.tilEmail)).setTypeface(Typeface.createFromAsset(
getAssets(), getString(R.string.app_font)));
editText.setTypeface(Typeface.SERIF);