首页 > 解决方案 > 如何在用户按下 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 进行垃圾邮件。也许最好的方法是在按下按钮后添加延迟或在短时间内禁用按钮?

标签: javaandroidimagebutton

解决方案


在您的点击监听器中尝试倒数计时器

new CountDownTimer(5000, 1000) {

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

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

}.start();

推荐阅读