如何降低EditText
提示大小?的Android的EditText提示尺寸
回答
可以減少對EditText
字體大小 - 這會降低hint
的大小爲好。即android:textSize="16sp"
謝謝你,你救了我一些頭疼! :) – peterkodermac 2014-10-23 18:59:47
很高興我能幫忙! :) – inky 2014-10-24 22:55:09
很好的答案,而不是所有這個解決方法:D – Tony 2015-04-18 17:20:56
我也必須這樣做,因爲我的提示不符合標準尺寸的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);
}
}
});
什麼是EVSearchTerm? – MGDroid 2012-12-11 12:24:24
@EVSearchTerm是他的edittext參考。命名慣例被遺棄。 – Javanator 2014-11-17 12:47:50
這種方法有效。這裏的Android SDK的問題在於,對於提示文本大小,以編程方式設置文本大小完全被忽略。另一方面,這可以讓longhairedsi的方法得到簡化。您可以直接調用'editText.setTextSize(TypedValue.COMPLEX_UNIT_SP,mRealTextSize);'來代替添加'TextWatcher'。然而,longhairedsi的解決方案可能會更安全。我很好奇這個問題是否會在較新的SDK版本中修復。 – 2016-09-06 14:33:55
您可以設置簡單的HTML屬性的提示字符串本身。
見接受的答案在這裏: Android EditText hint
編輯:剛打了我自己,這個工作對我來說:
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 (雖然沒有爲我工作)
很容易降低edittext的提示大小
editText.setHint(Html.fromHtml(
"<font size=\"5\">" + "hinttext1" + "</font>" +
"<small>" + "hinttext2" + "</small>"));
@marmor的方法是最好的一個。您可以更改<small> --- </small>
標籤的數量以調整尺寸。
您也可以直接定義提示的文字和我一樣
view.setHint(Html.fromHtml("<small><small><small>" + "This is Hint" + "</small></small></small>"));
希望這會有所幫助。
如果你想以編程方式做到這一點,
SpannableString span = new SpannableString(strHint);
span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(span);
@ user2982553的解決方案,我的偉大工程。您也可以使用AbsoluteSizeSpan
,您可以使用它設置提示的確切字體大小。請勿使用<font size=\"5\">
標記,因爲size
屬性僅被忽略。
你可以通過在字符串recource中設置一個大小來實現。
例如:
<string name="edittext_hint"><font size="15">Hint here!</font></string>
然後在你的XML只是寫
android:hint="@string/edittext_hint"
這將在一個較小的文字提示,但對於輸入文本的原始大小resault。
希望這有助於爲未來的讀者
如何爲''size''屬性指定''sp''尺寸? – 2015-08-05 08:21:28
像魅力一樣工作 – 2015-09-18 00:37:05
因爲你沒有在這裏指定像sp這樣的角錢,難道不是屏幕尺寸不同的問題嗎? 我沒有找到任何方法來通過佈局xml設置提示大小,而不會導致尺寸更改時出現問題 – 2015-10-03 17:20:07
我需要設置一個較大的尺寸比實際的提示文字。
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;
}
}
您不僅可以更改提示的大小,還可以更改其字體和樣式。我實現它使用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);
- 1. Android EditText佈局寬度全尺寸
- 2. Android EditText提示
- 3. Android的微調文字的尺寸從提示
- 4. Android Widget尺寸
- 5. Android ViewPager尺寸
- 6. 而不提尺寸
- 7. 如何在使用尺寸提示時獲得Kivy佈局的絕對尺寸
- 8. qTip 2提示尺寸問題
- 9. 更改Android的ListView尺寸
- 10. Android中的圖片尺寸
- 11. Android圖標的尺寸
- 12. Android圖像的尺寸
- 13. 在Android的屏幕尺寸
- 14. android的屏幕尺寸
- 15. Android小部件的尺寸?
- 16. Android的錯屏幕尺寸
- 17. Android的圖像尺寸
- 18. Android Widget錯誤的尺寸
- 19. Android上的Cardview尺寸
- 20. Android Auto Scrolling EditText提示
- 21. 顯示的圖像尺寸與所採用的尺寸相同?
- 22. 如何讓iPhone尺寸的網頁顯示正確的尺寸?
- 23. Android屏幕尺寸
- 24. Android Bitmap降尺寸
- 25. Android屏幕尺寸
- 26. Android N顯示器尺寸退出
- 27. Android圖片不顯示全尺寸
- 28. Android EditText垂直尺寸:我希望它很小!
- 29. Android中的EditText提示錯誤
- 30. 更改屏幕尺寸的按鈕尺寸Android Studio
我認爲沒有必要調整提示的大小。你爲什麼想這樣做? – Praveen 2010-06-29 10:38:13
提示字體大小是非常大的..所以我需要從它減少2sp .. – Sasikumar 2010-06-30 05:22:48
我有同樣的問題。我們正在實施撥號應用程序,因此撥號的號碼可能很大,而提示文本需要更小以適應一切。感謝@Jeka提供的解決方案! – Bradford2000 2016-06-24 18:20:12