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

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

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

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

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

编辑:因为EdiText内部使用ErrorPopup显示提示弹出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

没有公开的方法来访问EditText的提示字体,但我们可以通过reflection来实现它。

在下面的示例中,我们获取EditText的提示Layout字段,我们可以从中复制TextPaint ,在副本上设置所需的Typeface ,然后在提示的Layout上设置该实例。

 private void setHintTypeface(EditText editText, Typeface typeFace) { try { int hintColor = edit.getHintTextColors().getDefaultColor(); Field hintLayoutField = TextView.class.getDeclaredField("mHintLayout"); hintLayoutField.setAccessible(true); Layout hintLayout = (Layout) hintLayoutField.get(edit); TextPaint hintPaint = new TextPaint(hintLayout.getPaint()); hintPaint.setTypeface(typeFace); hintPaint.setColor(hintColor); Field paintField = Layout.class.getDeclaredField("mPaint"); paintField.setAccessible(true); paintField.set(hintLayout, hintPaint); } catch (NoSuchFieldException | IllegalAccessException e) { e.printStackTrace(); } } 

我假设你想在获得对EditText的引用后立即设置字体。 在这种情况下,我们需要推迟调用上面的方法,直到EditText自行放置,并实例化其成员对象。 我们可以通过简单地将Runnable发布到调用setHintTypeface()方法的Handler来实现。

 edit = (EditText) findViewById(R.id.edit_text); edit.post(new Runnable() { @Override public void run() { setHintTypeface(edit, Typeface.DEFAULT); } } ); 

上面的代码只是使用了该类中可用的预定义静态Typeface之一,但您应该能够使用您所拥有的任何自定义Typeface

下面是用于快速测试EditText的XML。

  

如果您需要在多个实例上使用此行为,这可以很容易地集成到自定义EditText子类中,但如果您只需要更改一个,则使用该方法似乎是更简单的解决方案。

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