【问题标题】:Android EditText hint uses the same font that the EditText hasAndroid EditText 提示使用与 EditText 相同的字体
【发布时间】:2016-01-15 06:08:56
【问题描述】:

我已经为 EditText 定义了一种字体,现在 EditText 提示也显示该字体,但我需要为 EditText 提示使用不同的字体,有没有办法实现这一点?

【问题讨论】:

  • EditText的字体可以改,hint的字体不能改。
  • 你可以做一件事动态改变文本大小。在 textchange 监听器上
  • @playmaker420 之前没有看到那个帖子,无论如何谢谢
  • 这不是重复的问题。投票支持重新开放。
  • AFAIK 字体适用于所有文本,提示无法分开

标签: java android android-layout android-xml


【解决方案1】:

Android EditText 提示使用与 EditText 相同的字体

EditText 使用textview_hint 布局显示ErrorPopup 弹出消息。所以尝试为Hint 设置不同的字体为:

 LayoutInflater inflater = LayoutInflater.from(<Pass context here>);
 TextView hintTextView = (TextView) inflater.inflate(
                        com.android.internal.R.layout.textview_hint, null);
 hintTextView.setTypeface(< Typeface Object>);

编辑: 因为EdiText 在内部使用ErrorPopup 显示提示弹出窗口。

查看以下链接获取更多帮助:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/5.1.1_r1/android/widget/Editor.java#Editor.invalidateTextDisplayList%28%29

【讨论】:

  • 嗯,OP在哪里对错误弹出窗口说了什么?
  • @MikeM.:EditText 在内部使用ErrorPopup 来显示提示消息。
  • @MikeM.:答案中缺少什么?
  • 不,我只是以为 OP 是在谈论褪色的背景提示,而不是弹出窗口; like here.
【解决方案2】:

无需使用反射。您可以通过创建自己的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);
    }
}

将其用作:

// set font to EditText itself
Typeface editTextTypeface = Typeface.createFromAsset(getAssets(), "font1.ttf");
EditText editText = (EditText) findViewById(R.id.normalEditText);
editText.setTypeface(editTextTypeface);

// set font to Hint
Typeface hintTypeface = Typeface.createFromAsset(getAssets(), "font2.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);

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 1970-01-01
    • 2017-08-25
    • 2021-03-26
    • 1970-01-01
    • 2013-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多