Android EditText 힌트 크기


86

EditText힌트 크기 를 줄이는 방법은 무엇입니까?


2
힌트의 크기를 조정할 필요가 없다고 생각합니다. 왜 그렇게 하시겠습니까?
Praveen 2010-06-29

4
내가 그것에서 2SP 줄일 필요가 그래서 힌트 글꼴 크기는 ... 아주 큰 ...
Sasikumar

1
나는 같은 문제가 있었다. 다이얼러 응용 프로그램을 구현하여 전화를 건 번호가 상당히 클 수있는 반면 힌트 텍스트는 모든 것에 맞도록 더 작아야합니다. 솔루션에 대한 @Jeka에게 감사드립니다!
Bradford2000

1
<문자열 이름 = "이름"> <폰트 크기 = "20"> 이름 </ FONT> <작은> (문서 당 기준) </ 작은> </ 문자열>
Sanket Parchande

답변:


129

문자열 회수에서 크기를 설정하여 할 수 있습니다.

예를 들면 :

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

그런 다음 XML에서

android:hint="@string/edittext_hint"

이것은 힌트의 경우 더 작은 텍스트로 재 탐색되지만 입력 텍스트의 경우 원래 크기입니다.

이것이 미래의 독자들에게 도움이되기를 바랍니다.


1
속성 의 sp차원 을 지정하는 방법은 size무엇입니까?
f_ficarola

1
여기서 sp와 같은 치수를 지정하지 않았으므로 화면 크기가 다르면 문제가되지 않습니까? 치수가 변경 될 때 문제를 일으키지 않고 레이아웃 xml을 통해 힌트 크기를 설정하는 방법을 찾지 못했습니다.
Vishwanath gowda k

2
힌트가 프로그래밍 방식으로 설정할 때이 작동하지 않습니다
애쉬 윈 마하 잔

60

의 글꼴 크기를 줄일 수 있습니다. 그러면 EditText의 크기도 줄어 듭니다 hint. 즉android:textSize="16sp"


감사합니다. 두통을 덜어 주셨습니다! :)
peterkodermac 2014 년

도와 드릴 수있어서 기쁩니다! :)
inky

약 대신이 모든 일의 대 답 : D
Antwan

33
하나는 실제 텍스트 용이고 다른 하나는 힌트 용으로 두 가지 다른 크기를 원하면 어떻게합니까?
f_ficarola

꽤 현명한 대답 이었지만 위의 의견은 정확합니다. 하나는 실제 텍스트 용이고 다른 하나는 힌트 용입니다.
Arslan Ali Awan

36

내 힌트가 표준 크기의 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 참조입니다. 명명 규칙이 폐기되었습니다.
Rohit Sharma 2014

1
이 접근 방식이 효과적입니다. 여기서 Android SDK의 문제는 힌트 텍스트 크기에 대해 프로그래밍 방식으로 텍스트 크기를 설정하는 것이 완전히 무시된다는 것입니다. 다른 한편으로는 longhairedsi의 접근 방식을 단순화 할 수 있습니다. 추가하는 대신 즉시 TextWatcher전화를 걸 수 있습니다 editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, mRealTextSize);. 그러나 longhairedsi의 솔루션은 미래에 더 안전 할 수 있습니다. 이 문제가 최신 SDK 버전에서 수정 될지 궁금합니다.
Peter 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 (하지만 저에게는 작동하지 않았습니다)


12

프로그래밍 방식으로 수행하려면

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.setHint(Html.fromHtml(
    "<font size=\"5\">" + "hinttext1" + "</font>" + 
    "<small>" + "hinttext2" + "</small>" )); 

2

@marmor의 접근 방식이 가장 좋습니다. <small> --- </small>태그 수를 변경하여 크기를 조정할 수 있습니다.

내가했던 것처럼 Hint의 텍스트를 직접 정의 할 수도 있습니다.

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

이것이 도움이되기를 바랍니다.


1

@ user2982553의 솔루션이 저에게 잘 맞습니다. 을 사용 AbsoluteSizeSpan하여 힌트의 정확한 글꼴 크기를 설정할 수도 있습니다. <font size=\"5\">태그를 사용하지 마십시오 size. 속성이 무시되기 때문입니다.


1

값 폴더의 strings.xml에서 이것을 정의하십시오.

<string name="enter_otp"><font size="16">your text</font></string>

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)
            }
        }
    }
}
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.