ANDROID:如何把延迟的按钮被按下后? [英] ANDROID: How to put a delay after a button is pushed?
问题描述
我有一个按钮,当它是pressed它起着一个音频文件。我希望把上的按钮5秒钟时间,因此用户不会捣碎按钮,反复播放声音。我想我真的想为按钮5秒钟禁用它被推后。有谁知道如何做到这一点?
I have a button and when it is pressed it plays an audio file. I want to put a 5 second delay on the button so users wont mash the button and play the sound over and over. I guess what i really want it for the button to be disabled for 5 seconds after it is pushed. Does anyone know how to do this?
推荐答案
在您onClickListener的按钮:
In your onClickListener for the button:
myButton.setEnabled(false);
Timer buttonTimer = new Timer();
buttonTimer.schedule(new TimerTask() {
@Override
public void run() {
runOnUiThread(new Runnable() {
@Override
public void run() {
myButton.setEnabled(true);
}
});
}
}, 5000));
点击后这将禁用键,5秒后再次启用它。
This will disable the button when clicked, and enable it again after 5 seconds.
如果点击事件在扩展视图,而不是在一个活动做同样的事情,但替换类处理 runOnUiThread
与后
。
If the click event is handled in class that extends View rather than in an Activity do the same thing but replace runOnUiThread
with post
.
这篇关于ANDROID:如何把延迟的按钮被按下后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!