简体   繁体   English

向Progress Dialog添加延迟

[英]Add a delay to Progress Dialog

I want to make a dummy progress dialog appear for 2 or 3 seconds. 我想让虚拟进度对话框出现2或3秒钟。 It won't actually do anything other than say detecting. 除了说检测之外,它实际上不会做任何事情。 I have the code: 我有代码:

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

    dialog.dismiss();

But what do I put in between the show, and the dismissal to have the dialog appear for a few seconds? 但是我在演出和解雇之间放置了什么让对话框显示几秒钟? Thanks! 谢谢!

The correct way - it does not block your main thread, so UI stays responsive: 正确的方法 - 它不会阻止您的主线程,因此UI保持响应:

dialog.show();

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

You can also use CountDownTimer of Android which is much more efficient than any other solution posted and it also support fires on regular interval through its onTick() method. 您还可以使用Android的CountDownTimer ,它比任何其他发布的解决方案更有效,并且它还通过onTick()方法定期支持火灾

Have a look at this example, 看看这个例子,

 new CountDownTimer(3000, 1000) {

     public void onTick(long millisUntilFinished) {
             // You don't need anything here
     }

     public void onFinish() {
         dialog.dismiss();
     }
  }.start();

You can do something like this: 你可以这样做:

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();

Nothing comes between dismiss and show.. the time depending upon you. 在解雇和表演之间没有任何事情......时间取决于你。 For example dialog will show before server access and it will dismiss getting result means 例如,对话框将在服务器访问之前显示,它将忽略获取结果的方法

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

If you need empty delay means then use CountDownTimer call between that.. 如果你需要空延迟意味着然后在那之间使用CountDownTimer调用..

In case some one looking for Xamarin.Android ( using C# ) solution ( of Peter Knego's answer ), here is how: 如果有人在寻找Xamarin.Android使用C# )解决方案( Peter Knego的答案 ),这里是如何:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM