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

前端 未结 3 806
你的背包
你的背包 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:10

    Here you go.

    ((Button) findViewById(R.id.click))
        .setOnClickListener(new OnClickListener() {
    
        @Override
        public void onClick(View v) {
            ((Button) findViewById(R.id.click)).setEnabled(false);
    
            new Handler().postDelayed(new Runnable() {
    
                @Override
                public void run() {
                    ((Button) findViewById(R.id.click))
                        .setEnabled(true);
    
                }
            }, 5000);
    
        }
    });
    

提交回复
热议问题