如何设置EditText的“textColorHint”属性的动画?

时间:2017-02-07 19:36:08

标签: android android-edittext android-animation

我想在编辑文本中为提示的颜色设置动画,但以下似乎不起作用。

fun animateHintTextColorChange(et: EditText, colorFromResId: Int, colorToResId: Int) {
        val colorFrom = ContextCompat.getColor(et.context, colorFromResId)
        val colorTo = ContextCompat.getColor(et.context, colorToResId)
        val animator = ObjectAnimator.ofInt(et, "textColorHint", colorFrom, Color.RED)
        animator.setEvaluator(ArgbEvaluator())
        animator.start()
    }

2 个答案:

答案 0 :(得分:0)

创建CustomSpan

import android.text.TextPaint;
import android.text.style.CharacterStyle;
import android.text.style.UpdateAppearance;


public class CustomSpan extends CharacterStyle implements UpdateAppearance {

    private int color;

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
    }

    public CustomSpan() {

    }

    @Override
    public void updateDrawState(TextPaint paint) {
        paint.setColor(color);

    }
}

然后使用ObjectAnimator

    final EditText editText = (EditText) this.findViewById(R.id.edit_text);

    CustomSpan span = new CustomSpan();

    final String text = getResources().getString(R.string.edit_hint);

    final SpannableString spannableString = new SpannableString(text);

    int start = 0;
    int end = text.length();
    spannableString.setSpan(span, start, end, 0);



    int colorFrom = Color.BLACK;
    int colorTo = Color.RED;
    int duration = 2000;

    final ObjectAnimator objectAnimator = ObjectAnimator.ofInt(
            span, CHANGE_COLOR_PROPERTY,  colorFrom, colorTo);

    objectAnimator.setEvaluator(new ArgbEvaluator());
    objectAnimator.setDuration(duration);
    objectAnimator.start();

    objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
        @Override
        public void onAnimationUpdate(ValueAnimator animation) {
           editText.setHint(spannableString);
        }
    });

CustomProperty

private static final Property<CustomSpan, Integer> CHANGE_COLOR_PROPERTY
        = new Property<CustomSpan, Integer>(Integer.class, "CHANGE_COLOR_PROPERTY") {

    @Override
    public void set(CustomSpan span, Integer value) {
        span.setColor(value);
    }
    @Override
    public Integer get(CustomSpan object) {
        return object.getColor();
    }
};

注意:如果您的持续时间很慢,您可以看到一些从开始颜色到结束颜色的中间颜色。

更新:我还建议查看MutableForegroundSpan @ http://flavienlaurent.com/blog/2014/01/31/spans/

答案 1 :(得分:0)

由于@Nikola Despotoski的提示,我找到了解决方案。

<input type="submit" value="Chat Now" onclick="handleForm(input)">

[...]
function handleForm(input) {
  var isValid = validateForm(input);
  if (isValid) {
    //perform your ajax request here
  }
}

function validateForm(input) { ...