Различный стиль шрифта подсказки и набранный в текстовом стиле шрифта Android - PullRequest
7 голосов
/ 13 октября 2011

Я хочу иметь другой стиль шрифта для подсказки и стиль шрифта для набираемого текста в тексте редактирования.Например,Допустим, размер шрифта подсказки равен 12 и его обычный тип.Но когда пользователь начинает вводить текст редактирования, размер шрифта набираемого текста должен стать 14 и жирным шрифтом.еще раз, если пользователь удалит текст, подсказка должна быть вышеупомянутого типа.

Ответы [ 3 ]

4 голосов
/ 15 марта 2012

Вы можете программно изменить цвет подсказки, чтобы он отличался от стиля шрифта, введенного в edittext, используя следующий код

    editTextId.setHintTextColor(Color.alpha(006666));
0 голосов
/ 02 августа 2017

Вы можете достичь этого, используя 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);
0 голосов
/ 12 августа 2014

Уже дан правильный ответ, но в настоящее время указать другой цвет можно также в файле XML с помощью атрибута android: textColorHint . Например, вы можете сделать что-то подобное (при условии, что вы правильно определили свой my_favourite_colour как ресурс):

<EditText
... other properties here ...
android:textColorHint="@color/my_favourite_colour"
</EditText>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...