Android EditText提示大小


83

如何减小EditText提示大小?


2
我认为没有必要调整提示的大小。你为什么要这样做?
Praveen

4
提示字体大小是非常大的。所以我需要从它减少2SP ..
Sasikumar

1
我有同样的问题。我们正在实现一个Dialer应用程序,因此拨打的号码可能会很大,而提示文本则需要较小以适合所有内容。感谢@Jeka提供解决方案!
Bradford2000

1
<string name =“ name”> <font size =“ 20”>名称</ font> <small>(根据文档)</ small> </ string>
Sanket Parchande

Answers:


128

您可以通过在字符串资源中设置大小来实现。

例如:

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

然后在您的XML中编写

android:hint="@string/edittext_hint"

这将以较小的文本重新提示提示,但以原始大小重新输入。

希望这对将来的读者有所帮助


1
如何spsize属性指定尺寸?
f_ficarolaola 2015年

1
由于您没有在此处指定像sp这样的尺寸,因此在不同的屏幕尺寸下会不会有问题?我找不到任何方法来通过布局xml设置提示大小,而不会在尺寸更改时引起问题
Vishwanath gowda k 2015年

2
以编程方式设置提示时,此方法不起作用
ashwin mahajan

60

您可以在EditText-上减小字体大小,这也会减小的大小hint。即android:textSize="16sp"


谢谢,您让我省去了一些麻烦!:)
peterkodermac

很高兴我能帮助你!:)
漆黑

一个好的答案,而不是所有这些工作:D
Antwan

33
如果我想要两种不同的大小,一种用于真实文本,另一种用于提示,该怎么办?
f_ficarola15年

很酷,这是一个很聪明的答案,但是上面的注释是正确的,如果我想要两种不同的大小,一种用于真实文本,另一种用于提示呢?
Arslan Ali Awan

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是他的edittext参考。放弃命名约定。
Javanator 2014年

1
这种方法有效。Android SDK的问题在于,对于提示文本大小,完全以编程方式设置文本大小会被忽略。另一方面,这可以简化longhairsi的方法。您无需直接添加TextWatcher即可直接添加editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);。但是,longhairsi的解决方案将来可能更安全。我很好奇这个问题是否可以在更新的SDK版本中解决。
彼得F

谢谢。不要忘记设置初始文本大小(否则,开始后文本为空,提示的大小可能等于文本大小)。
CoolMind

31

您可以将简单的HTML属性设置为提示字符串本身。

在此处查看已接受的答案: Android EditText提示

编辑:我自己玩过,对我有用:

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

喜欢您的解决方案,但不幸的是无法正常工作,它从资源加载并将其动态设置为TextInputLayout
JimmyFlash

9

减少edittext的提示大小很容易

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

您不仅可以更改提示的大小,还可以更改其字体和样式。我实现了使用SpannableString和解决它MetricAffectingSpan

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)
            }
        }
    }
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.