根据EditText焦点播放Rotate动画时,第一次进入页面所有动画都自动开始

今天开发公司项目,界面中有很多EditText,根据这些EditText获取焦点与否,来决定是哪个ImageView来进行旋转动画。
项目截图

需求是刚进入界面时,第一个图片旋转,接下来,根据选择的EditText不同,决定哪个ImageView进行旋转,可是开发时需要比较尴尬的事,刚一进入界面时,只有第一个EditText获取了焦点,可是却所有动画都开始。代码片段如下:

初始化控件动画:

private void initView() {
   ivParamsetOneManual = (ImageView)findViewById(R.id.iv_paramset_one_manual);
   etParamsetFanspeedOneManual = (EditText)findViewById(R.id.et_paramset_fanspeed_one_manual);
   etParamsetFanspeedOneManual.setOnFocusChangeListener(this);
   etParamsetOilfrequencyOneManual = (EditText)findViewById(R.id.et_paramset_oilfrequency_one_manual);
   etParamsetOilfrequencyOneManual.setOnFocusChangeListener(this);
   ivParamsetTwoManual = (ImageView)findViewById(R.id.iv_paramset_two_manual);
   etParamsetFanspeedTwoManual = (EditText)findViewById(R.id.et_paramset_fanspeed_two_manual);
   etParamsetFanspeedTwoManual.setOnFocusChangeListener(this);
   etParamsetOilfrequencyTwoManual = (EditText)findViewById(R.id.et_paramset_oilfrequency_two_manual);
   etParamsetOilfrequencyTwoManual.setOnFocusChangeListener(this);
    }

设置焦点监听:

@Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch(v.getId()){
            case R.id.et_paramset_fanspeed_one_manual:
            case R.id.et_paramset_oilfrequency_one_manual:
                if (hasFocus) {
                    Log.d(TAG, "=====one manual get the focus=====");
                    ivParamsetOneManual.startAnimation(rotate);
                } else {
                    Log.d(TAG, "=====one manual loss the focus=====");
                    ivParamsetOneManual.clearAnimation();
                }
                break;
            case R.id.et_paramset_fanspeed_two_manual:
            case R.id.et_paramset_oilfrequency_two_manual:
                if (hasFocus) {
                    Log.d(TAG, "=====two manual get the focus=====");
                    ivParamsetTwoManual.startAnimation(rotate);
                } else {
                    Log.d(TAG, "=====two manual loss the focus=====");
                    ivParamsetTwoManual.clearAnimation();
                }
                break;
        }
    }

后来将(1)、(2)代码移到焦点监听函数中,才解决了这个问题

@Override
    public void onFocusChange(View v, boolean hasFocus) {
        switch(v.getId()){
            case R.id.et_paramset_fanspeed_one_manual:
            case R.id.et_paramset_oilfrequency_one_manual:
                if (hasFocus) {
                    Log.d(TAG, "=====one manual get the focus=====");
                    ivParamsetOneManual.setAnimation(rotate);//(1)设置动画1
                    ivParamsetOneManual.startAnimation(rotate);
                } else {
                    Log.d(TAG, "=====one manual loss the focus=====");
                    ivParamsetOneManual.clearAnimation();
                }
                break;
            case R.id.et_paramset_fanspeed_two_manual:
            case R.id.et_paramset_oilfrequency_two_manual:
                if (hasFocus) {
                    Log.d(TAG, "=====two manual get the focus=====");
                    ivParamsetTwoManual.setAnimation(rotate);//(2)设置动画2
                    ivParamsetTwoManual.startAnimation(rotate);
                } else {
                    Log.d(TAG, "=====two manual loss the focus=====");
                    ivParamsetTwoManual.clearAnimation();
                }
                break;
        }
    }

注:虽然我也不知道原因是什么,但是希望和我有一样问题的小伙伴能从我的文章中得到一些启示或通过读到这篇文章解决自己的问题

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值