EditText
フィールドに表示されるヒントのフォントを変更することはできますか?私はXML自体にフォントを設定したい。editTextヒントのフォントを変更
答えて
これは、質問に対する誤った答えです。私は削除を試みたが、私は司会者以外はできない。親切に答えを無視するか、答えを自由に更新してください。この答えは誤解を招きます。ごめんなさい :)。
editText.setTypeface(Typeface.SERIF);
あなただけのTextView
ようEditText
のフォントを変更することができます。しかし、hint
の書体を変更することはできません。 XMLで
そのことはできませんが -
テキストとヒントはXMLだけで同じフォントを持つことができます。
私はあなたがこのように達成することができますXML.Butでのヒントのフォントを変更するには、任意の有用な方法を見つけることがあります:
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) {
}
});
シンプルな解決策は、 – Darius
あなたがSpannableStringとカスタムTypefaceSpanでそれを変更することができます。
まず、カスタムTypefaceSpanクラスを作成します。
TypefaceSpan typefaceSpan = new CustomTypefaceSpan(typeface);
SpannableString spannableString = new SpannableString(hintText);
spannableString.setSpan(typefaceSpan, 0, spannableString.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
そして最後に、ちょうどあなたのEditTextのヒント設定:
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);
}
}
は、それからちょうどSpannableStringにTypefaceSpanを設定
mEditText.setHint(spannableString);
を
である必要があります。これは正解です。ありがとう@francisco_ssb。それは完全に動作します。 –
非常に簡単な方法があります。私はちょうど私のアプリケーションでやったと働いた。 キーはEditTextと共にTextInputLayoutの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)));
は魅力的に機能します。 – JCasso
このlibraryを使用してヒントフォントを変更できます。 francisco_ssbさん@
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
ライブラリをコンパイルした後は、アプリケーションのクラスとクラス定義は、次のコマンドを作成する必要があります。 CalligraphyConfig.initDefault(新CalligraphyConfig.Builder() .setDefaultFontPath(「font.ttf」) .setFontAttrId(R.attr.fontPath) .build() );あなたは、次のコマンドでそれをオーバーライドする各アクティビティ後 : @Override 保護のボイドattachBaseContext(コンテキストnewBase){ super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase)); } –
@ johnny5、この回答は、私がこの回答が参照するカリグラフィーライブラリを使用していたときに助けてくれました。 HOSHYAR Ahmadpourも答えを説明した。答えHOSHYAR Ahmadpourありがとうございました。 – Sakiboy
@Sakiboyええ、それは将来の人がこの答えを使うことができるように私はレビューキューにいた、私は彼が答えを固定した後に彼をupvoted –
:あなたは、次のコマンドでそれをオーバーライドする各アクティビティの後
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("font.ttf")
.setFontAttrId(R.attr.fontPath)
.build()
);
:
ライブラリをコンパイルした後は、アプリケーションのクラスとクラス定義は、次のコマンドを作成する必要があります答えは正しい。しかし、私は、ヒントのフォントだけでなく、そのサイズとスタイルを変更するのに役立つ代替のソリューションを提供します。私はこのソリューションが役立つことを願っています。
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);
カスタム
MetricAffectingSpan
オブジェクトを作成します
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):
1)は、カスタムHint
オブジェクトを作成します。 10
使用このコード:
edittext.setAccentTypeface(typeface);
これはEditTextでは利用できません。 –
- 1. アンドロイドレイアウトでEditText内のヒントとテキストのフォントを変更するには?
- 2. Edittextでフォントが変更されない
- 3. TextInputLayoutのEditTextヒントのサイズを変更する方法
- 4. Android EditTextヒント
- 5. アンドロイドEDITTEXTヒント問題
- 6. EditTextの恒久的なヒント
- 7. 複数の行にヒントがあるEditText
- 8. edittextカーソルの色を変更
- 9. WinApiフォントの変更
- 10. Android EditTextの幅の変更
- 11. EditTextヒントが表示されない
- 12. Webアプリケーションインターフェイスの最適化 - フォント/ヒント?
- 13. EditTextの自動サイズ変更
- 14. JLabelのフォントを変更
- 15. フォントのスタイルを変更
- 16. テーブルヘッダーセクションのフォントを変更
- 17. PythonのIDEヒントを変更する
- 18. の変更フォントの太さ
- 19. フォントの変更のWebView AndroidStudio
- 20. JavaScriptのサイズ変更フォント
- 21. DevExpressとC#フォントの変更
- 22. 変更フォントの色は
- 23. フォントとフォントサイズの変更
- 24. フォント設定の変更
- 25. フォントの変更は、リッチテキストボックス
- 26. 動的フォントのサイズ変更
- 27. jupyterlab - スタイリングの変更 - フォント、フォントサイズ
- 28. EditTextを別のEditTextに変更してください
- 29. EditTextのデザインを変更しました
- 30. EditTextキャンバスのdrawTextを変更します。
可能重複:http://stackoverflow.com/questions/4716959/setting-typeface-of-hint-in-textedit – thepoosh
指摘したように、これは重複しています。ヒントの色は変更できますが、書体は変更できません。 – jsmith
あなたは私の答え[ここ](http://stackoverflow.com/a/40695930/3578171)それが動作することを確認することができます。 –