ANDROID:如何把延迟的按钮被按下后?按下、按钮、ANDROID

由网友(草莓果酱.)分享简介:我有一个按钮,当它是pressed它起着一个音频文件。我希望把上的按钮5秒钟时间,因此用户不会捣碎按钮,反复播放声音。我想我真的想为按钮5秒钟禁用它被推后。有谁知道如何做到这一点?I have a button and when it is pressed it plays an audio file. I want...

我有一个按钮,当它是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.

阅读全文

相关推荐

最新文章