Android编辑文本提示大小
如何减小Android编辑文本提示大小,android,Android,如何减小EditText提示大小?您可以减小EditText上的字体大小,这也将减小提示的大小。i、 e.android:textSize=“16sp”我也不得不这样做,因为我的提示不适合标准大小的编辑文本。所以我这样做了(在xml中将textSize设置为mhintextsize): 可以将简单的HTML属性设置为提示字符串本身 请参见此处接受的答案: 编辑:我自己刚玩过,这对我很有用: view.setHint(Html.fromHtml("<small><small&g
EditText
提示大小?您可以减小EditText
上的字体大小,这也将减小提示的大小。i、 e.android:textSize=“16sp”
我也不得不这样做,因为我的提示不适合标准大小的编辑文本。所以我这样做了(在xml中将textSize设置为mhintextsize):
可以将简单的HTML属性设置为提示字符串本身
请参见此处接受的答案:
编辑:我自己刚玩过,这对我很有用:
view.setHint(Html.fromHtml("<small><small><small>" +
getString(R.string.hint) + "</small></small></small>"));
view.setHint(Html.fromHtml(“”+
getString(R.string.hint)+“”);
这是fromHtml接受的标记列表:
(虽然对我不起作用)减少编辑文本的提示大小很容易
editText.setHint(Html.fromHtml(
"<font size=\"5\">" + "hinttext1" + "</font>" +
"<small>" + "hinttext2" + "</small>" ));
editText.setHint(Html.fromHtml(
“+”hinttext1“+”
“+”hinttext2“+”);
@marmor的方法是最好的。您可以更改--
标记的数量以调整大小
您也可以像我一样直接定义提示的文本
view.setHint(Html.fromHtml(“”+
这是提示“+”)代码>
希望这会有所帮助。如果您希望以编程方式进行
SpannableString span = new SpannableString(strHint);
span.setSpan(new RelativeSizeSpan(0.5f), 0, strHint.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
editText.setHint(span);
@user2982553的解决方案对我来说非常有用。您还可以使用绝对大小span
,使用它可以设置提示的确切字体大小。不要使用
标记,因为size
属性被忽略。您可以通过在字符串资源中设置一个大小来实现
例如:
<string name="edittext_hint"><font size="15">Hint here!</font></string>
这将为提示重新生成较小的文本,但为输入文本生成原始大小
希望这对未来的读者有所帮助我需要为真实文本设置一个比提示更大的大小
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)创建自定义提示
对象:
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);
}
}
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);
}
}
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);
2)创建自定义MetricAffectingSpan
对象:
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);
}
}
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);
}
}
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);
3)使用:
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);
}
}
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);
}
}
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);
使用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)
}
}
}
}
在您的strings.xml in values文件夹中定义:
<string name="enter_otp"><font size="16">your text</font></string>
您的文本
我认为没有必要调整提示的大小。为什么要这样做?提示字体大小非常大。。所以我需要减少2sp,我也有同样的问题。我们正在实现一个拨号程序应用程序,以便拨号号码可以相当大,而提示文本需要更小才能适合所有内容。感谢@Jeka提供的解决方案!姓名(根据文件)谢谢,你帮我省去了一些麻烦很高兴我能帮忙!:)很好的答案,而不是所有这些工作:如果我想要两个不同的大小,一个用于真实文本,另一个用于提示?很酷,这是一个非常聪明的答案,但上面的评论是正确的。如果我想要两个不同的大小,一个用于真实文本,另一个用于提示呢?@EVSearchTerm是他的edittext参考。命名约定被抛弃。这种方法有效。Android SDK的问题在于,对于提示文本大小,完全忽略了以编程方式设置文本大小。另一方面,这使得longhairedsi的方法得以简化。您只需调用editText.setTextSize(TypedValue.COMPLEX\u UNIT\u SP,mRealTextSize),而不是添加TextWatcher
代码>立即。然而,longhairedsi的解决方案可能更安全。我很好奇这个问题是否会在更新的SDK版本中得到解决。谢谢。不要忘记设置初始文本大小(否则,当开始后文本为空时,提示的大小可能等于文本大小)。如何为size
属性指定sp
维度?因为您在这里没有指定像sp这样的维度,在不同的屏幕大小中会不会出现问题?我找不到任何方法通过布局xml设置提示大小,但在维度更改时不会引起问题当提示设置为ProgrammaticallyLove您的解决方案时,这不起作用,但不幸的是没有起作用,从资源加载并将其动态设置为TextInputLayout