android 如何更改提示文本大小而不更改EditText中的文本大小

u3r8eeie  于 2023-06-27  发布在  Android
关注(0)|答案(5)|浏览(169)

我有一个EditText输入字段。我在里面加了一个提示。现在我想改变提示文本的大小,但是当我这样做的时候,它也会影响文本的大小。请指导我如何分别改变提示和文本的大小,并为提示和文本给予不同的字体。

<EditText
    android:layout_width="0dp"
    android:layout_height="50dp"
    android:layout_weight="1"
    android:textSize="12sp"
    android:textColor="#ffffff"                            
    android:fontFamily="sans-serif-light"
    android:hint="MM/YY"
    android:textColorHint="@color/white" />
nkkqxpd9

nkkqxpd91#

可以在资源文件中设置。
例如:

<string name="hint"><font size="20">Hint!</font></string>

您的XML:

android:hint="@string/hint"
v9tzhpje

v9tzhpje2#

提示和文本是互斥的,如果其中一个可见,则另一个不可见。
因此,您可以根据EditText是否为空(提示可见)或不为空(文本可见)来更改其属性。
例如:

final EditText editText = (EditText) findViewById(R.id.yourEditText);

editText.addTextChangedListener(new TextWatcher() {
    boolean hint;

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        if(s.length() == 0) {
            // no text, hint is visible
            hint = true;
            editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
            editText.setTypeface(Typeface.createFromAsset(getAssets(),
                "hintFont.ttf")); // setting the font
        } else if(hint) {
            // no hint, text is visible
            hint = false;
            editText.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
            editText.setTypeface(Typeface.createFromAsset(getAssets(),
                "textFont.ttf")); // setting the font
        }
    }

    @Override
    public void afterTextChanged(Editable s) {
    }
});
zbdgwd5y

zbdgwd5y3#

使用HTML是可以的,但它不灵活。例如,您无法设置确切的大小。我将提供一个替代解决方案,您可以设置:

  • 新提示字体
  • 新提示大小
  • 新提示样式
    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);
ttp71kqs

ttp71kqs4#

您可以将文本大小设置为较小的所需值,然后设置文本侦听器以在输入某些文本后更改文本大小。

sshcrbum

sshcrbum5#

你只需要使用"app:hintTextAppearance="@style/TextInputLayoutHintText"到你的TextInputEditText,在风格上保持你所需要的任何大小
例如:<style name="TextInputLayoutHintText"> <item name="android:textSize">@dimen/text_size_7</item> </style>

相关问题