2010-06-29 52 views
45

如何降低EditText提示大小?的Android的EditText提示尺寸

+2

我認爲沒有必要調整提示的大小。你爲什麼想這樣做? – Praveen 2010-06-29 10:38:13

+3

提示字體大小是非常大的..所以我需要從它減少2sp .. – Sasikumar 2010-06-30 05:22:48

+0

我有同樣的問題。我們正在實施撥號應用程序,因此撥號的號碼可能很大,而提示文本需要更小以適應一切。感謝@Jeka提供的解決方案! – Bradford2000 2016-06-24 18:20:12

回答

41

可以減少對EditText字體大小 - 這會降低hint的大小爲好。即android:textSize="16sp"

+0

謝謝你,你救了我一些頭疼! :) – peterkodermac 2014-10-23 18:59:47

+0

很高興我能幫忙! :) – inky 2014-10-24 22:55:09

+0

很好的答案,而不是所有這個解決方法:D – Tony 2015-04-18 17:20:56

33

我也必須這樣做,因爲我的提示不符合標準尺寸的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? – MGDroid 2012-12-11 12:24:24

+2

@EVSearchTerm是他的edittext參考。命名慣例被遺棄。 – Javanator 2014-11-17 12:47:50

+0

這種方法有效。這裏的Android SDK的問題在於,對於提示文本大小,以編程方式設置文本大小完全被忽略。另一方面,這可以讓longhairedsi的方法得到簡化。您可以直接調用'editText.setTextSize(TypedValue.COMPLEX_UNIT_SP,mRealTextSize);'來代替添加'TextWatcher'。然而,longhairedsi的解決方案可能會更安全。我很好奇這個問題是否會在較新的SDK版本中修復。 – 2016-09-06 14:33:55

8

很容易降低edittext的提示大小

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

@marmor的方法是最好的一個。您可以更改<small> --- </small>標籤的數量以調整尺寸。

您也可以直接定義提示的文字和我一樣

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

希望這會有所幫助。

6

如果你想以編程方式做到這一點,

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

@ user2982553的解決方案,我的偉大工程。您也可以使用AbsoluteSizeSpan,您可以使用它設置提示的確切字體大小。請勿使用<font size=\"5\">標記,因爲size屬性僅被忽略。

73

你可以通過在字符串recource中設置一個大小來實現。

例如:

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

然後在你的XML只是寫

android:hint="@string/edittext_hint" 

這將在一個較小的文字提示,但對於輸入文本的原始大小resault。

希望這有助於爲未來的讀者

+1

如何爲''size''屬性指定''sp''尺寸? – 2015-08-05 08:21:28

+0

像魅力一樣工作 – 2015-09-18 00:37:05

+1

因爲你沒有在這裏指定像sp這樣的角錢,難道不是屏幕尺寸不同的問題嗎? 我沒有找到任何方法來通過佈局xml設置提示大小,而不會導致尺寸更改時出現問題 – 2015-10-03 17:20:07

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