【问题标题】:Add a delay to Progress Dialog向进度对话框添加延迟
【发布时间】:2011-05-11 16:34:45
【问题描述】:

我想让一个虚拟的进度对话框出现 2 或 3 秒。除了说检测之外,它实际上不会做任何事情。我有代码:

    ProgressDialog dialog = ProgressDialog.show(this, "", "Detecting...",
            true);
    dialog.show();

    dialog.dismiss();

但是我在节目和解雇之间放了什么让对话框出现几秒钟?谢谢!

【问题讨论】:

    标签: android delay progressdialog


    【解决方案1】:

    正确的方法——它不会阻塞你的主线程,所以 UI 保持响应:

    dialog.show();
    
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {
        public void run() {
            dialog.dismiss();
        }
    }, 3000); // 3000 milliseconds delay
    

    【讨论】:

    • 很好的答案,只是你在最后一行之前缺少了一个额外的花括号:} }, 3000); // 3000 毫秒
    • 对不起!我们如何在等待时更新进度对话框中的消息。例如:5 秒... 4 秒... 3 秒... 2 秒... 1 秒... BOOOOM!并解雇
    【解决方案2】:
            progress.setProgress(100);
            Handler handler = new Handler();
            handler.postDelayed(new Runnable() {
                public void run() {
                    pdialog.dismiss();
                }}, 3000);
    

    【讨论】:

      【解决方案3】:

      您还可以使用 Android 的 CountDownTimer,它比发布的任何其他解决方案都更有效,并且它还通过其 onTick() 方法支持定期触发 .

      看看这个例子,

       new CountDownTimer(3000, 1000) {
      
           public void onTick(long millisUntilFinished) {
                   // You don't need anything here
           }
      
           public void onFinish() {
               dialog.dismiss();
           }
        }.start();
      

      【讨论】:

      • 这正是我需要的,汉德功能不能在 Leanback 中使用:D
      【解决方案4】:

      你可以这样做:

      new AsyncTask<Void, Void, Void>
      {
          ProgressDialog dialog = new ProgressDialog(MainActivity.this);
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
      
              dialog.setTitle("Please wait");
              dialog.setMessage("Preparing resources...");
              dialog.setCancelable(false);
              dialog.show();
          }
      
      
          @Override
          protected Void doInBackground(Void... params) {
              try{
                 Thread.sleep(3000);
              } 
              catch(Exception e)
              {
                  e.printStackTrace();
              }
          }
      
          @Override
          protected void onPostExecute(Void aVoid) 
          {
              super.onPostExecute(aVoid);
              if (dialog!=null && dialog.isShowing())
              {
                  dialog.dismiss();
              }
          }
      }.execute();
      

      【讨论】:

        【解决方案5】:

        在解雇和展示之间没有任何关系。时间取决于你。例如,对话框将在服务器访问之前显示,它会关闭获取结果的方式

         dialog.show();
         ServerCall();
         dialog.close();
        

        如果您需要空延迟意味着在这之间使用 CountDownTimer 调用..

        【讨论】:

          【解决方案6】:

          如果有人在寻找Xamarin.Android使用 C#)解决方案(Peter Knego's answer),方法如下:

          new Handler().PostDelayed(() => { 
              dialog.dismiss();
          }, 1000);
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2011-07-29
            • 2013-07-07
            • 2012-11-25
            • 2015-09-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多