简体   繁体   English

ProgressDialog 继续显示,尽管被取消和隐藏

[英]ProgressDialog keeps on showing despite being cancelled and hidden

Right now I'm simulating the showing of a ProgressDialog for an event that is expected to take several seconds.现在,我正在为预计需要几秒钟的事件模拟 ProgressDialog 的显示。

I'm doing it this way:我是这样做的:

progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage(getString(R.string.calendar_load));
progressDialog.setCancelable(false);
progressDialog.show();
Thread t=new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    Thread.sleep(6000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                getActivity().runOnUiThread(new Runnable()
                {
                    @Override
                    public void run() {

                        progressDialog.cancel();
                        progressDialog.hide();

But even though I've checked in debug that the progressDialog.cancel() and progressDialog.hide() execute the dialog just keeps on showing apparently in an indefinite way.但是,即使我已经在调试中检查了 progressDialog.cancel() 和 progressDialog.hide() 执行对话框,它仍然明显地以不确定的方式继续显示。

What could be causing such behavior?什么可能导致这种行为?

PROBLEM SOLVED: Thanks to everyone who has answered/commented, it looks like an emulator bug (indeed it has also worked some times on emulator).问题已解决:感谢所有回答/评论的人,它看起来像一个模拟器错误(实际上它也曾在模拟器上工作过一段时间)。

Call progressDialog.dismiss();调用progressDialog.dismiss();

Could you try this snippet?你能试试这个片段吗?

progressDialog.show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        while (progressDialog.getProgress() <= progressDialog.getMax()) {
                            Thread.sleep(100);
                            handle.sendMessage(handle.obtainMessage());
                            if (progressDialog.getProgress() == progressDialog.getMax()) {
                                progressDialog.dismiss();
                            }
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();

            Handler handle = new Handler() {
                    @Override
                    public void handleMessage(Message msg) {
                        super.handleMessage(msg);
                        progressDialog.incrementProgressBy(1);
                    }
            };

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

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