单击时如何展开/折叠EditText?

时间:2019-08-27 11:14:02

标签: java android animation android-edittext

我想创建一个动画来在单击EditText时将其展开(达到我设置的高度),而当它失去焦点(我单击其他内容)时,它应该折叠回一行。这是我尝试过的方法,但它什么都不做。

public class CollapseDownAnimation extends Animation {
    private EditText mDescription;
    private boolean mDown;

    public CollapseDownAnimation(EditText description, boolean down) {
        this.mDescription=description;
        this.mDown=down;
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation transformation) {
        int newHeight;
        if(mDown) {
            newHeight=(int) (300 * interpolatedTime);
        } else {
            newHeight=(int) (300 * (1-interpolatedTime));
        }
        mDescription.getLayoutParams().height=newHeight;
        mDescription.requestLayout();
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
    }

    @Override
    public boolean willChangeBounds() {
        return true;
    }
}

EditText mDescription;
mDescription = view.findViewById(R.id.description);
        CollapseDownAnimation anim = new CollapseDownAnimation(mDescription, mDescription.hasFocus());
        anim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        Log.d(TAG, "the focus is "+mDescription.hasFocus());
        mDescription.setAnimation(anim);

0 个答案:

没有答案