在Android上制作动画时如何设置按钮不可点击?

时间:2012-05-16 05:01:25

标签: android button

translate.animate进行过程中如何使按钮无法点击。我尝试使用button.setclickable(false)button.setEnabled(false)两个dint为我工作。

这是我的代码:

b2.setOnClickListener(new OnClickListener()
{
  public void onClick(View v)
  {
    if(i%2==0)
    {
      //b2.setClickable( false );
      //            Toast toast1 = Toast.makeText(getBaseContext(), "Success", 1);
      //            toast1.show();
      if(i==0)
      {
        //b2.setOnClickListener(null);
        b2.setEnabled(false);
        //b2.setClickable(false);
        int modifierY;
        modifierY=-70;
        Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
        translateAnimation.setDuration(600);
        translateAnimation.setFillEnabled(true);
        MyAnimationListener listener=new MyAnimationListener(b2, modifierY,SimpleViewPagerforAndroidActivity.this);
        translateAnimation.setAnimationListener(listener);
        // b2.setOnClickListener(null);
        Animation translateAnimation1=new TranslateAnimation(0, 0, 0,0 );
        translateAnimation1.setDuration(1000);
        translateAnimation1.setFillEnabled(true);
        MyAnimationListener listener1=new MyAnimationListener(main, 0,SimpleViewPagerforAndroidActivity.this);
        translateAnimation1.setAnimationListener(listener1);

        main.setVisibility(RelativeLayout.VISIBLE);
        /*  long time =AnimationUtils.currentAnimationTimeMillis();

        main.invalidate(); 
        b2.invalidate(); 

        translateAnimation.setStartTime(time);
        translateAnimation1.setStartTime(time);*/
        main.startAnimation(translateAnimation1);

        b2.startAnimation(translateAnimation);
        i++;
        //   b2.setOnClickListener(this);
      }
      else
      {
        //b2.setOnClickListener(null);
        b2.setEnabled(false);
        //b2.setClickable(false);
        int modifierY;
        modifierY=-70;
        Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
        translateAnimation.setDuration(1000);
        translateAnimation.setFillEnabled(true);
        MyAnimationListener listener=new MyAnimationListener(b2, modifierY,SimpleViewPagerforAndroidActivity.this);
        translateAnimation.setAnimationListener(listener);

        Animation translateAnimation1=new TranslateAnimation(0, 0, 0,-150 );
        translateAnimation1.setDuration(600);
        translateAnimation1.setFillEnabled(true);
        MyAnimationListener listener1=new MyAnimationListener(main, -150,SimpleViewPagerforAndroidActivity.this);
        translateAnimation1.setAnimationListener(listener1);

        main.setVisibility(RelativeLayout.VISIBLE);
        /*   long time =AnimationUtils.currentAnimationTimeMillis();

        main.invalidate(); 
        b2.invalidate(); 

        translateAnimation.setStartTime(time);
        translateAnimation1.setStartTime(time);*/
        main.startAnimation(translateAnimation1);

        b2.startAnimation(translateAnimation);
        //   b2.setOnClickListener(this);
        i++;
      }
    }
    else 
    {
      //b2.setOnClickListener(null);
      b2.setEnabled(false);
      //b2.setClickable(false);


      Animation translateAnimation=new TranslateAnimation(0, 0, 0,150 );
      translateAnimation.setDuration(1000);
      translateAnimation.setFillEnabled(true);
      MyAnimationListener listener=new MyAnimationListener(main, 150,SimpleViewPagerforAndroidActivity.this);
      translateAnimation.setAnimationListener(listener);
      Animation translateAnimation1=new TranslateAnimation(0, 0, 0,70 );
      translateAnimation1.setDuration(600);
      translateAnimation1.setFillEnabled(true);
      MyAnimationListener listener1=new MyAnimationListener(b2, 70,SimpleViewPagerforAndroidActivity.this);
      translateAnimation1.setAnimationListener(listener1);
      /* long time =AnimationUtils.currentAnimationTimeMillis();

      main.invalidate(); 
      b2.invalidate(); 

      translateAnimation.setStartTime(time);
      translateAnimation1.setStartTime(time);
      main.startAnimation(translateAnimation1);

      b2.startAnimation(translateAnimation);*/
      b2.startAnimation(translateAnimation1);
      main.startAnimation(translateAnimation);
      i++;
      //b2.setOnClickListener(this);
    }
  }
});

3 个答案:

答案 0 :(得分:22)

选中按钮点击动画是否完成,使用hasEnd属性,检查以下代码:

if (!(animation.hasEnded()) {
  button.setClickable(false);  
}
else{
 button.setClickable(true);  
}

答案 1 :(得分:4)

我使用button.setEnabled(false)来停用触控事件。

答案 2 :(得分:0)

首先使按钮可选。 然后这些事情会奏效。

  

Button.setSelected(真);