AndroidEditTextヒントサイズ


83

EditTextヒントサイズを減らす方法は?


2
ヒントのサイズを変更する必要はないと思います。なぜあなたはそれをしたいのですか?
Praveen 2010年

4
ヒントフォントサイズが非常に大きいので、2spを減らす必要があります。–
Sasikumar

1
私も同じ問題を抱えていました。ダイヤラアプリケーションを実装しているので、ダイヤル番号をかなり大きくすることができますが、ヒントテキストはすべてに合うように小さくする必要があります。解決策をくれた@Jekaに感謝します!
Bradford2000

1
<string name = "name"> <
fontsize

回答:


128

文字列リソースにサイズを設定することでそれを行うことができます。

例えば:

<string name="edittext_hint"><font size="15">Hint here!</font></string>

次に、XMLに次のように記述します。

android:hint="@string/edittext_hint"

これにより、ヒントのテキストは小さくなりますが、入力テキストの元のサイズになります。

これが将来の読者に役立つことを願っています


1
属性のspディメンションを指定するにはどうすればよいsizeですか?
f_ficarola 2015

1
ここではspのような寸法を指定していないので、画面サイズが異なっていても問題ありませんか?寸法が変更されたときに問題を引き起こさずにレイアウトxmlを介してヒントサイズを設定する方法が見つかりません
Vishwanath gowda k 2015

2
ヒントがプログラムで設定されている場合、これは機能しません
ashwin mahajan 2018

60

EditText-のフォントサイズを小さくすることができhintます。これにより、のサイズも小さくなります。すなわちandroid:textSize="16sp"


ありがとう、あなたは私にいくつかの頭痛を救った!:)
peterkodermac 2014年

お役に立てて嬉しいです!:)
真っ黒な2014年

代わりに、このすべての
回避策の

33
2つの異なるサイズが必要な場合はどうなりますか?1つは実際のテキスト用で、もう1つはヒント用です。
f_ficarola 2015

かっこいい答えでしたが、上記のコメントは正しいです。実際のテキスト用とヒント用の2つの異なるサイズが必要な場合はどうすればよいですか?
Arslan Ali Awan 2016年

35

ヒントが標準サイズのEditTextに収まらなかったため、これも行う必要がありました。だから私はこれをしました(xmlでtextSizeをmHintTextSizeに設定します):

MYEditText.addTextChangedListener(new TextWatcher(){

                @Override
                public void afterTextChanged(Editable arg0) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void beforeTextChanged(CharSequence arg0, int arg1,
                        int arg2, int arg3) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onTextChanged(CharSequence arg0, int start, int before,
                        int count) {
                    if (arg0.length() == 0) { 
                        // No entered text so will show hint
                        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mHintTextSize);
                    } else {
                        editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);
                    }
                }
        });

2
@EVSearchTermは彼の編集テキストリファレンスです。命名規則は見捨てられました。
javanator 2014年

1
このアプローチは機能します。ここでのAndroidSDKの問題は、プログラムでテキストサイズを設定しても、ヒントテキストサイズが完全に無視されることです。一方、これにより、longhairedsiのアプローチを簡素化できます。を追加する代わりに、すぐにTextWatcher呼び出すことができますeditText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);。ただし、longhairedsiのソリューションは将来的に安全になる可能性があります。この問題が新しいSDKバージョンで修正されるかどうか知りたいです。
ピーターF

ありがとう。初期テキストサイズを設定することを忘れないでください(そうしないと、開始後にテキストが空になると、ヒントのサイズがテキストサイズと等しくなる可能性があります)。
CoolMind 2018

31

単純なHTML属性をヒント文字列自体に設定できます。

ここで受け入れられた答えを参照してください: AndroidEditTextヒント

編集:自分で遊んだだけで、これは私のために働いた:

view.setHint(Html.fromHtml("<small><small><small>" + 
             getString(R.string.hint) + "</small></small></small>"));

これはfromHtmlによって受け入れられたタグのリストです:http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html (私には機能しませんでしたが)


11

プログラムで実行したい場合は、

SpannableString span = new SpannableString(strHint);
span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(span);

あなたのソリューションを
気に入った

9

エディットテキストのヒントサイズを簡単に減らすことができます

editText.setHint(Html.fromHtml(
    "<font size=\"5\">" + "hinttext1" + "</font>" + 
    "<small>" + "hinttext2" + "</small>" )); 

2

@marmorのアプローチは最高のものです。<small> --- </small>タグの数を変更してサイズを調整できます。

ヒントのテキストを私が行ったように直接定義することもできます

view.setHint(Html.fromHtml("<small><small><small>" + "This is Hint" + "</small></small></small>"));

これがお役に立てば幸いです。


1

@ user2982553のソリューションは私にとって素晴らしい働きをします。AbsoluteSizeSpanヒントの正確なフォントサイズを設定できるを使用することもできます。<font size=\"5\">タグは使用しないでくださいsize。'属性は無視されるだけです。



0

ヒントよりも実際のテキストのサイズを大きく設定する必要があります。

public static class LargeSizeTextWatcher implements TextWatcher {

    private final EditText mEditText;
    private final int mOriginalSize;
    private final int mLargeSize;

    private int mLastLength;

    TrackingNumberTextWatcher(EditText editText) {
        mEditText = editText;
        mOriginalSize = (int) editText.getTextSize();
        mLargeSize = editText.getResources().getDimensionPixelSize(R.dimen.text_size_large);

        mLastLength = editText.length();
        if (mLastLength != 0) {
            mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLargeSize);
        }
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
        int length = s.length();
        if (length == 0) {
            mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mOriginalSize);
        } else if (mLastLength == 0) {
            mEditText.setTextSize(TypedValue.COMPLEX_UNIT_PX, mLargeSize);
        }
        mLastLength = length;
    }
}

0

ヒントのサイズだけでなく、フォントやスタイルも変更できます。私が使用してそれを解決するためになさSpannableStringMetricAffectingSpan

1)カスタムHintオブジェクトを作成します。

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)カスタムMetricAffectingSpanオブジェクトを作成します。

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);

0

onFocusChanged()リスナーを使用してヒントフォントサイズを変更することもオプションです。ユーザーがテキストフィールドをクリックしたときにaddTextChangeListener()がトリガーされず、点滅するカーソルがヒントフォントにサイズ変更されるためです。

また、TextChangeListenerとは異なり、初期ヒントフォントサイズを個別に設定する必要はありません。

class EditTextWithHintSize {
 init {
        val typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.EditTextWithHintSize, 0, defStyle)
        try {
            hintFontSize = typedArray.getDimension(R.styleable.EditTextWithHintSize_hint_font_size, textSize)
            fontSize = textSize

            if (length() == 0) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, hintFontSize)
            }
        } catch (e: Exception) {
            hintFontSize = textSize
            fontSize = textSize
        } finally {
            typedArray.recycle()
        }
    }

    override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        super.onFocusChanged(focused, direction, previouslyFocusedRect)

        if (focused) {
            setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
        } else {
            if (length() == 0) {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, hintFontSize)
            } else {
                setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSize)
            }
        }
    }
}
弊社のサイトを使用することにより、あなたは弊社のクッキーポリシーおよびプライバシーポリシーを読み、理解したものとみなされます。
Licensed under cc by-sa 3.0 with attribution required.