Android 如何使图像按钮在5秒钟后出现在主xml上?
我有一个淡入动画,在尝试了所有东西之后,我似乎无法正确使用Android 如何使图像按钮在5秒钟后出现在主xml上?,android,timer,imagebutton,Android,Timer,Imagebutton,我有一个淡入动画,在尝试了所有东西之后,我似乎无法正确使用定时器或定时器任务,因为我的应用程序崩溃了。五秒钟后,我希望我的按钮在五秒钟后显示或淡入。 我如何做到这一点 这是我的代码: ImageButton hit = (ImageButton) findViewById(R.id.tryButton2); final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein); h
定时器
或定时器任务
,因为我的应用程序崩溃了。五秒钟后,我希望我的按钮在五秒钟后显示或淡入。
我如何做到这一点
这是我的代码:
ImageButton hit = (ImageButton) findViewById(R.id.tryButton2);
final Animation animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fadein);
hit.startAnimation(animationFadeIn);
handler不工作,只是让它看起来不褪色,我希望这有帮助
hit.setVisibility(View.GONE);
animationFadeIn.setStartOffset(5000);
animationFadeIn.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
hit.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// Nothing to do here
}
@Override
public void onAnimationEnd(Animation animation) {
// Nothing to do here
}
});
hit.startAnimation(animationFadeIn);
编辑:
这里有一个非常快速和简单的方法来启用/禁用单击按钮:
可以使用布尔变量:
private boolean hasClicked = false
现在将其添加到OnClickListener中:
if(!hasClicked) {
hasClicked = true;
// call your animate method
}
现在您必须再次“启用”按钮。你必须在你的AnimationListener中找到这个
@凌驾
onAnimationEnd上的公共无效(动画){
hasClicked=false;
}
如果有帮助,请不要忘记upvote:)你需要在任何人都能提供帮助之前添加代码,更不用说回答了。另外,当询问应用程序崩溃时,发布堆栈跟踪。当我使用timer和timertask时,它会崩溃。你有没有在处理程序上尝试过
postrunable
方法?我刚刚使用了setEnabled(false);