如何在用户按下imageButton之间增加延迟?

时间:2019-04-09 11:25:12

标签: java android imagebutton

我想这样做,以便用户必须等待一小段时间才能允许按下imageButton,我不知道这样做的最佳方法。

private class HandleClick implements View.OnClickListener {

    public void onClick(View arg0) {

        if(arg0.getId()==R.id.imageButton){
            ((TextView) findViewById(R.id.textView2)).setText("Pressed: " + ++howManyClicks1);
            /* Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=1");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);*/
            simpleWebView.loadUrl("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=1");
            //startActivity(intent);

        }
        else if (arg0.getId()==R.id.imageButton1){
            ((TextView) findViewById(R.id.textView3)).setText("Pressed: " + ++howManyClicks2);
            /*Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=2");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            //startActivity(intent);*/
            simpleWebView.loadUrl("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=2");
        }
        else if (arg0.getId()==R.id.imageButton2){
            ((TextView) findViewById(R.id.textView5)).setText("Pressed: " + ++howManyClicks3);
            /*Uri uri = Uri.parse("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=3");
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            //startActivity(intent);*/
            simpleWebView.loadUrl("https://abr.se/happyeno/?get=happyeno_svar_put&svar_id=3");

        }
    }
}

我不希望用户能够通过点击imageButtons来发送垃圾邮件。也许最好的方法是在按下按钮后添加延迟或在短时间内禁用按钮?

3 个答案:

答案 0 :(得分:1)

在点击侦听器中尝试倒数计时器

new CountDownTimer(5000, 1000) {

   public void onTick(long millisUntilFinished) {
      //count down 5,4,3,2,1.
   }

   public void onFinish() {
      //Write you logic here
   }

}.start();

答案 1 :(得分:0)

尝试这种方式

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // Magic here
        }
    }, 1000); // Millisecond 1000 = 1 sec

或者您可以看看this Link

答案 2 :(得分:0)

它将在5秒钟后设置OnClickListener。您还可以立即设置OnClickListener,然后将图像设置为启用。

void setListeners() {
    imageView.postDelayed(new Runnable() {
        @Override
        public void run() {
            imageView.setOnClickListener(this);
        }
    }, 5 * 1000);
    //or like that
    new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
        @Override
        public void run() {
            imageView.setOnClickListener(this);
        }
    }, 5 * 1000);
}