【发布时间】:2011-06-10 15:48:19
【问题描述】:
是否可以在不更改 EditText 本身的字体的情况下设置 EditText 视图的提示元素的字体?谢谢。
【问题讨论】:
标签: android user-interface view
是否可以在不更改 EditText 本身的字体的情况下设置 EditText 视图的提示元素的字体?谢谢。
【问题讨论】:
标签: android user-interface view
好像没有办法分别控制提示的字体和文本的字体。
【讨论】:
你检查android:typeface了吗?
这对于 EditText 的提示和文本都适用。
【讨论】:
不知道您是否只是需要让它们不同,或者您是否想专门设置字体。
如果您只需要设置与文本不同的提示样式,以下是在 Java 中基于每个字段的方法
将fromHtml()的结果传递给setHint();
myField.setHint(Html.fromHtml("<i>" + hint + "</i>"));
【讨论】:
如果有人仍然需要解决这个问题:
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) {}
})
【讨论】:
您可以通过覆盖字体字体来设置提示字体
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);
}
}
【讨论】:
为了拥有特定的字体,有一个很棒的库 here ,它非常简单,但是要拥有一个带有 android:inputType="textPassword" 的编辑文本,你应该做更多的事情,所以这里是:
在代码中设置密码转换:
password.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); password.setTransformationMethod(PasswordTransformationMethod.getInstance());
【讨论】:
您应该创建 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);
}
}
【讨论】:
处理此问题的最佳方法是在 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);
【讨论】: