2012-05-16 73 views
7

如何在translate.animate正在進行時使按鈕不可點擊。我試着用button.setclickable(false)button.setEnabled(false)這兩個辛苦工作給我。如何在Android上進行動畫製作時設置按鈕不可點擊?

這裏是我的代碼:

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); 
    } 
    } 
}); 
+1

請發佈代碼段 – Rajesh

+0

使用AsyncTask這是當你需要做一些背景,而另一個任務正在運行,.... – SilentKiller

+0

你已經嘗試在動畫回調函數中設置它。 –

回答

16

檢查按鈕點擊動畫是否完成與否,使用hasEnd財產,檢查下面的代碼:

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

首先使按鈕選擇。 然後那些東西就會起作用。

Button.setSelected(true);

2

我用button.setEnabled(false)禁用觸摸事件。

相關問題