ANDROID: How to put a delay after a button is pushed?

前端 未结 3 816
你的背包
你的背包 2021-01-02 02:32

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

3条回答
  •  悲哀的现实
    2021-01-02 03:09

    You can disable your button, then use the postDelayed method on your button.

    myButton.setEnabled(false);
    myButton.postDelayed(new Runnable() {
        @Override
        public void run() {
            myButton.setEnabled(true);
        }
    }, 5000);
    

    This is similar to the Timer solution, but it might better handle configuration change (for example if the user rotate the phone)

提交回复
热议问题