ANDROID:如何把延迟的按钮被按下后? [英] ANDROID: How to put a delay after a button is pushed?

查看:300
本文介绍了ANDROID:如何把延迟的按钮被按下后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆