android edittext hint值,Android EditText Hint Size

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

How to reduce EditText Hint size?

回答1:

You can do it by setting a size in the string recource.

For example:

Hint here!

then in your XML just write

android:hint="@string/edittext_hint"

This will resault in a smaller text for the hint but the original size for the input text.

Hopes this helps for future readers

回答2:

You can reduce the font size on the EditText - that will reduce the size of the hint as well. i.e. android:textSize="16sp"

回答3:

I also had to do this as my hint didn't fit in the EditText at the standard size. So I did this (in the xml set textSize to 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);

}

}

});

回答4:

You can set simple HTML attributes to the hint string itself.

See accepted answer here:

Android EditText hint

EDIT: just played with it myself, this worked for me:

view.setHint(Html.fromHtml("" +

getString(R.string.hint) + ""));

This is a list of tags accepted by fromHtml:

http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html

(though didn't work for me)

回答5:

If you want to do it programmatically,

SpannableString span = new SpannableString(strHint);

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

editText.setHint(span);

回答6:

it is easy to reduce the hint size of the edittext

editText.setHint(Html.fromHtml(

"" + "hinttext1" + "" +

"" + "hinttext2" + "" ));

回答7:

@marmor 's Approach is the best One. You can alter the number of --- tags to adjust size.

You can also define the text of Hint directly as I did

view.setHint(Html.fromHtml("" +

"This is Hint" + ""));

Hope this will help.

回答8:

@user2982553 's solution works great for me. You could also use AbsoluteSizeSpan, with which you can set the exact font size of the hint. Don't use tag, 'cause the size attribute is just ignored.

回答9:

I need to set a larger size for real text than hint.

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;

}

}

回答10:

You can change not only a hint's size but also its font and style. I achieved to solve it using SpannableString and MetricAffectingSpan

1) Create a custom Hint object:

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) Create custom MetricAffectingSpan object:

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) Use:

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值