Android EditText 提示大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3139676/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android EditText Hint Size
提问by Sasikumar
How to reduce EditText
Hint size?
如何减少EditText
提示大小?
回答by Jeka
You can do it by setting a size in the string recource.
您可以通过在字符串资源中设置大小来实现。
For example:
例如:
<string name="edittext_hint"><font size="15">Hint here!</font></string>
then in your XML just write
然后在你的 XML 中写
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
希望这对未来的读者有所帮助
回答by inky
You can reduce the font size on the EditText
- that will reduce the size of the hint
as well. i.e. android:textSize="16sp"
您可以减小 上的字体大小EditText
- 这也会减小 的大小hint
。IEandroid:textSize="16sp"
回答by longhairedsi
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):
我也必须这样做,因为我的提示不适合标准大小的 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);
}
}
});
回答by marmor
You can set simple HTML attributes to the hint string itself.
您可以为提示字符串本身设置简单的 HTML 属性。
See accepted answer here: Android EditText hint
在此处查看已接受的答案: Android EditText 提示
EDIT: just played with it myself, this worked for me:
编辑:我自己玩过,这对我有用:
view.setHint(Html.fromHtml("<small><small><small>" +
getString(R.string.hint) + "</small></small></small>"));
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)
这是 fromHtml 接受的标签列表:http://commonsware.com/blog/Android/2010/05/26/html-tags-supported-by-textview.html (虽然对我不起作用)
回答by user2982553
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);
回答by ManiTeja
it is easy to reduce the hint size of the edittext
很容易减少编辑文本的提示大小
editText.setHint(Html.fromHtml(
"<font size=\"5\">" + "hinttext1" + "</font>" +
"<small>" + "hinttext2" + "</small>" ));
回答by dmSherazi
@marmor 's Approach is the best One. You can alter the number of <small> --- </small>
tags to adjust size.
@marmor 的方法是最好的方法。您可以改变<small> --- </small>
标签的数量来调整大小。
You can also define the text of Hint directly as I did
你也可以像我一样直接定义 Hint 的文本
view.setHint(Html.fromHtml("<small><small><small>" +
"This is Hint" + "</small></small></small>"));
view.setHint(Html.fromHtml("<small><small><small>" +
"This is Hint" + "</small></small></small>"));
Hope this will help.
希望这会有所帮助。
回答by trojantale
@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 <font size=\"5\">
tag, 'cause the size
attribute is just ignored.
@user2982553 的解决方案对我很有用。您还可以使用AbsoluteSizeSpan
,您可以使用它来设置提示的确切字体大小。不要使用<font size=\"5\">
标签,因为该size
属性只是被忽略了。
回答by edwardaa
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;
}
}
回答by Ayaz Alifov
You can change not only a hint's size but also its font and style. I achieved to solve it using SpannableString
and MetricAffectingSpan
您不仅可以更改提示的大小,还可以更改其字体和样式。我实现了使用SpannableString
和解决它MetricAffectingSpan
1) Create a custom Hint
object:
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) Create custom MetricAffectingSpan
object:
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) Use:
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);